从函数内部、类内部、模块内部访问 Angular 服务

Accessing an Angular Service from within a function, inside a class, inside a module

本文关键字:内部 Angular 访问 服务 函数 模块      更新时间:2023-10-10
module Helper {
    export class ListController {
        static batchDelete(data) {
            // Do something with Angular's $http ...
            $http.post(data)
        }
    }
}
// On click function
Helper.ListController.batchDelete(toBeDeleted);

数组中删除数据工作正常,但是,我想从该函数中执行$http请求。如何在batchDelete()内访问 Angular 的$http服务?

您的代码示例:

 // Do something with Angular's $http ...
 $http.post(data)

您需要注射$http。执行此操作的理想方法是创建一个类即服务,然后让 angular 将服务注入到控制器中。

更多

我有一个关于这个主题的视频:https://www.youtube.com/watch?v=Yis8m3BdnEM