模块中未解决的依赖关系(Unresolved dependency in module)

编程入门 行业动态 更新时间:2024-10-23 07:26:09
模块中未解决的依赖关系(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!

更多推荐

本文发布于:2023-07-25 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   中未   关系   module   dependency

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!