在本文中,我们将带你了解angularjs–如何`inject“$window`对象到`config`在角在这篇文章中,我们将为您详细介绍angularjs–如何`inject“$window`对象到
在本文中,我们将带你了解angularjs – 如何`inject“$window`对象到`config`在角在这篇文章中,我们将为您详细介绍angularjs – 如何`inject“$window`对象到`config`在角的方方面面,并解答angularjs injector常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、angular js 的 Ng-idle 无法在 Windows 服务器中工作以显示未捕获的错误:[$injector:modulerr]、AngularJS $injector 依赖注入详解、angularJS $injector:modulerr 有效解决方案。
本文目录一览:- angularjs – 如何`inject“$window`对象到`config`在角(angularjs injector)
- $inject.instantiate VS $inject.get VS $injector.invoke in angularjs
- angular js 的 Ng-idle 无法在 Windows 服务器中工作以显示未捕获的错误:[$injector:modulerr]
- AngularJS $injector 依赖注入详解
- angularJS $injector:modulerr 有效解决方案
angularjs – 如何`inject“$window`对象到`config`在角(angularjs injector)
什么是正确的方法来做到这一点?
这里是我的代码:
angular.module('myApp',['$window']) //is this wrong? .config(function ($window) { //this is not the way? console.log($window); //console.log fails //error }) .controller("main",function($scope) { $scope.index = 0; $scope.num = number[$scope.index]; $scope.increase = function () { $scope.index += 1; $scope.num = number[$scope.index]; } })
Live Demo
angular.module('myApp',[]) .config(function ($windowProvider) { var $window = $windowProvider.$get(); console.log($window); })
$inject.instantiate VS $inject.get VS $injector.invoke in angularjs
app.service('myService',function ($q,$http) { return { q: $q,http: $http }; });
$injector.get(name,[caller]);
返回所请求服务的实例.
$injector.get('myService'); // { q: $q,http: $http }
$injector.invoke(fn,[self],[localals]);
调用提供的方法,并从$inject中传递给定的参数.
$injector.invoke(function (myService,$http) { console.log(myService); // { q: $q,http: $http }; console.log(this); // { v: 'im this!' }; console.log($http); // null },{ v: 'im this!' },{ $http: null });
$injector.instantiate(Type,[locals]);
创建给定类型的新实例.使用构造函数,然后使用构造函数注释中指定的参数调用新实例.
假设以下’class’:
function Person (fName,lName,$http,$q) { return { first_name: fName,last_name: lName,http: $http,q: $q } }
现在,如果我们想在我们的控制器中创建一个新的Person,我们可以这样做:
app.controller('...',function ($injector) { var $http = $injector.get('$http'); var $q = $injector.get('$q'); var p = new Person('kasper','lewau',$q); console.log(p); // { first_name: 'kasper',last_name: 'lewau',q: $q }; });
想象一下,人有〜20个依赖关系,我们用$inject.get方法获取每个人的每一个.
繁琐!而且 – 你需要保持你的参数&参数同步啊.
相反,你可以这样做:
app.controller('...',function ($injector) { var p = $injector.instantiate(Person,{ fName: 'kasper',lName: 'lewau' }); console.log(p); // { first_name: 'kasper',q: $q }; });
而且 – 如果我们想要,我们可以提供本地的.instantiate调用,以便覆盖内部$inject.get()在实例化时通常会得到的内容.
var p = $injector.instantiate(Person,{ fName: 'kasper',lName: 'lewau' },{ $http: 'nothing!',$q: 'nothing!' }); console.log(p); // { first_name: 'kasper',http: 'nothing!',q: 'nothing!' };
我希望解释三者之间的区别.如果您需要更多有关差异的信息,我会推荐这些文章:
> http://taoofcode.net/studying-the-angular-injector/
> http://taoofcode.net/studying-the-angular-injector-annotate/
> http://taoofcode.net/studying-the-angular-injector-invoke/
> http://taoofcode.net/studying-the-angular-injector-getservice/
> http://taoofcode.net/studying-the-angular-js-injector-instantiate/
angular js 的 Ng-idle 无法在 Windows 服务器中工作以显示未捕获的错误:[$injector:modulerr]
如何解决angular js 的 Ng-idle 无法在 Windows 服务器中工作以显示未捕获的错误:[$injector:modulerr]?
我们已经为会话超时实现了 Ng-Idle。当我们在本地运行它并包含所有依赖文件时,一切正常,但是当我们发布到我们的服务器时它不起作用。
The Errors we get when trying
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
AngularJS $injector 依赖注入详解
推断式注入
这种注入方式,需要在保证参数名称与服务名称相同。如果代码要经过压缩等操作,就会导致注入失败。
app.controller("myCtrl1", function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } });
标记式注入
这种注入方式,需要设置一个依赖数组,数组内是依赖的服务名字,在函数参数中,可以随意设置参数名称,但是必须保证顺序的一致性。
var myCtrl2 = function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } } myCtrl2.$injector = [''hello1'',''hello2'']; app.controller("myCtrl2", myCtrl2);
内联式注入
这种注入方式直接传入两个参数,一个是名字,另一个是一个数组。这个数组的最后一个参数是真正的方法体,其他的都是依赖的目标,但是要保证与方法体的参数顺序一致(与标记注入一样)。
app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } }]);
$injector常用的方法
在angular中,可以通过angular.injector()获得注入器。
var $injector = angular.injector();
通过$injector.get(''serviceName'')获得依赖的服务名字
$injector.get(''$scope'')
通过$injector.annotate(''xxx'')获得xxx的所有依赖项
$injector.annotate(xxx)
样例代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="myCtrl1"> <input type="button" ng-click="hello()" value="ctrl1"></input> </div> <div ng-controller="myCtrl2"> <input type="button" ng-click="hello()" value="ctrl2"></input> </div> <div ng-controller="myCtrl3"> <input type="button" ng-click="hello()" value="ctrl3"></input> </div> <script type="text/javascript"> var app = angular.module("myApp",[]); app.factory("hello1",function(){ return { hello:function(){ console.log("hello1 service"); } } }); app.factory("hello2",function(){ return { hello:function(){ console.log("hello2 service"); } } }); var $injector = angular.injector(); console.log(angular.equals($injector.get(''$injector''),$injector));//true console.log(angular.equals($injector.invoke(function($injector) {return $injector;}),$injector));//true //inferred // $injector.invoke(function(serviceA){}); app.controller("myCtrl1", function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } }); //annotated // function explicit(serviceA) {}; // explicit.$inject = [''serviceA'']; // $injector.invoke(explicit); var myCtrl2 = function($scope,hello1,hello2){ $scope.hello = function(){ hello1.hello(); hello2.hello(); } } myCtrl2.$injector = [''hello1'',''hello2'']; app.controller("myCtrl2", myCtrl2); //inline app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function($scope,hello1,hello2){ // app.controller("myCtrl3",[''$scope'',''hello1'',''hello2'',function(a,b,c){ // a.hello = function(){ // b.hello(); // c.hello(); // } $scope.hello = function(){ hello1.hello(); hello2.hello(); } }]); console.log($injector.annotate(myCtrl2));//["$scope","hello1","hello2"] </script> </body> </html>
以上就是对AngularJS injector的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
- angular 服务的单例模式(依赖注入模式下)详解
- 深入理解Angular中的依赖注入
- Angular 4依赖注入学习教程之InjectToken的使用(八)
- Angular 4依赖注入学习教程之ValueProvider的使用(七)
- AngularJS学习第二篇 AngularJS依赖注入
- AngularJS的依赖注入实例分析(使用module和injector)
- 自学实现angularjs依赖注入
- Angular.JS学习之依赖注入$injector详析
- 详解Angular依赖注入
angularJS $injector:modulerr 有效解决方案
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js"></script>
在出错的html页面中,加入如上的js即可!!!!!!
AngularJS的版本1.2.9
还有一种情况,是把script错误的当成了空标签. 即如下的写法是错误的:
<script type="text/javascript" src="angular.min.js"/>
script标签必须有开始和结束标签才行,如下才是正确的
<script type="text/javascript" src="angular.min.js"></script>
关于angularjs – 如何`inject“$window`对象到`config`在角和angularjs injector的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、angular js 的 Ng-idle 无法在 Windows 服务器中工作以显示未捕获的错误:[$injector:modulerr]、AngularJS $injector 依赖注入详解、angularJS $injector:modulerr 有效解决方案的相关信息,请在本站寻找。
本文标签: