模块中未解决的依赖关系(Unresolved dependency in module)
我在AngularJs1.3中得到一个$ injector:unpr和以下代码,我无法理解。
的index.html
<html lang="en" ng-app="directivesApp"> <head> <title>AngularJS Directive example</title> <script src="js/angular.min.js"></script> <script src="js/app.js"></script> </head> <body ng-controller="directiveCtrl"> <user-info user="MD"></user-info> <user-info user="VP"></user-info> </body> </html>app.js
angular.module('directivesApp', []) .controller("directiveCtrl",function($scope){ $scope.MD={ "firstName":"...","lastName":"..."}; $scope.VP={"firstName":"...","lastName":"..."}; }) .directive("userInfo",function($scope){ return{ restrict:'E', template:'User : <b>{{user.firstName}}</b> <b>{{user.lastName}}</b>', scope:{user : "="} }; });请帮助解决这个问题
I get a $injector:unpr with the following code in AngularJs1.3,that I am unable to fathom.
index.html
<html lang="en" ng-app="directivesApp"> <head> <title>AngularJS Directive example</title> <script src="js/angular.min.js"></script> <script src="js/app.js"></script> </head> <body ng-controller="directiveCtrl"> <user-info user="MD"></user-info> <user-info user="VP"></user-info> </body> </html>app.js
angular.module('directivesApp', []) .controller("directiveCtrl",function($scope){ $scope.MD={ "firstName":"...","lastName":"..."}; $scope.VP={"firstName":"...","lastName":"..."}; }) .directive("userInfo",function($scope){ return{ restrict:'E', template:'User : <b>{{user.firstName}}</b> <b>{{user.lastName}}</b>', scope:{user : "="} }; });Help in resolving this is appreciated
最满意答案
指令中的$scope通过link或controller方法函数访问,而不是在顶层注入。 尝试这个:
angular.module('directivesApp', []) .controller("directiveCtrl",function($scope){ $scope.MD={ "firstName":"...","lastName":"..."}; $scope.VP={"firstName":"...","lastName":"..."}; }) .directive("userInfo",function(){ return{ restrict:'E', link: function (scope) { // Scope using code here... }, template:'User : <b>{{user.firstName}}</b> <b>{{user.lastName}}</b>', scope:{user : "="} }; });这是一个Codepen。 希望这可以帮助!
$scope, within a directive, is accessed via the link or controller method functions, and isn't injected at the top-level. Try this:
angular.module('directivesApp', []) .controller("directiveCtrl",function($scope){ $scope.MD={ "firstName":"...","lastName":"..."}; $scope.VP={"firstName":"...","lastName":"..."}; }) .directive("userInfo",function(){ return{ restrict:'E', link: function (scope) { // Scope using code here... }, template:'User : <b>{{user.firstName}}</b> <b>{{user.lastName}}</b>', scope:{user : "="} }; });Here's a Codepen. Hope this helps!
更多推荐
发布评论