Wal*_*anG 6 javascript jquery angularjs
我想忘记"jQuery",因为我喜欢"AngularJS".但是,我需要知道如何使用在我的应用程序的其他地方包含AngularJS的独立任务.在这种情况下,我想使用"$ https AngularJS"函数来导入JavaScript文件.
示例(以前在jQuery中执行):
$.get("url.js", function(data){ eval(data) }); //ok
console.info($.get); //code code code... ok
Run Code Online (Sandbox Code Playgroud)
示例(如AngularJS中所述)
//In a controller
App.controller('Ctrllr', ['$http', function ($http) {
$http.get("url.js").success(function(data){
eval(data); //ok
});
console.info($http); //code code code.... ok
})
//outside
$http.get("url.js"); //$http is undefined
//How to use $http here?
Run Code Online (Sandbox Code Playgroud)
正如您在上次调用中看到的那样,$ http不在进程中.现在,想知道如何在控制器/应用程序之外使用类$ http或其他Angular工具?
你不能那样做。但没关系,你不应该这样做。
您应该做的是使用工厂设置一个服务,然后您的控制器或任何您想要的东西都可以访问该服务。工厂是这样的……
app.factory('service', ['$http', function($http) {
var service = {}
service.get = function() {
return $http.get('/url');
}
return service.
}]);
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此服务注入到您想要的任何内容中,并可以从那里调用 get 函数,该函数将为您返回承诺。
编辑:澄清一下,你不必回报你可以发挥你的想象力的承诺。
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |