我必须在angularjs 1.2中优化应用程序
这是代码玉器文件,我将服务动态传递到我的指令(creaDatos.jade)
div(datos-u, service="userDataSrv") // The div has associated a directive to which has service is dynamically passed
这是代码指令(datosU.js)
(appModule.lazy || appModule)
.directive('datosU', [ function() {
// Runs during compile
return {
restrict: 'A',
scope: {
service: '='
},
templateUrl: 'commons/html/user.html',
controller: 'userCtrl'
};
}]);
这是代码控制器(userCtrl.js)
(appModule.lazy || appModule)
.controller('userCtrl', ['$scope', '$injector',
function($scope, $injector) {
var srv = $injector.get($scope.service); /* The variable "srv" should have the value "userDataSrv" but his value is "undefined", The value of "$scope.service" is "undefined" */
}]);
这是浏览器控制台中的错误:
Error: [$injector:unpr] Unknown provider: undefinedProvider <-
http://errors.angularjs.org/1.2.16/$injector/unpr?p0=undefinedProvider%20%3C-%20
我不知道我在做什么错,请您能帮我吗?
答案 0 :(得分:1)
使用字符串绑定(service
)代替=
(@
)的双向绑定。
因此,将指令更改为此:
(appModule.lazy || appModule)
.directive('datosU', [ function() {
// Runs during compile
return {
restrict: 'A',
scope: {
service: '@'
},
templateUrl: 'commons/html/user.html',
controller: 'userCtrl'
};
}]);