在我的 Angular v1.4.8 应用程序中使用 ngRoute ,我有一个带有解析的路由,我想在自定义指令的链接函数中访问它。
根据文档:
For easier access to the resolved dependencies from the template, the resolve map will be available on the
scope
of the route, under$resolve
(by default) or a custom name specified by theresolveAs
property.
...但是,在我的自定义指令的链接函数中,$resolve
在 scope
属性上未定义 - 即使我观察它的更改也是如此。
为什么$resolve
在scope
上不可用?
代码: ( JSFiddle )
angular.module('myApp', ['ngRoute'])
.config(routeConfig)
.directive('myDirective', myDirective)
;
function routeConfig($routeProvider) {
$routeProvider
.when('/', {
resolve: {
data: function($q) {
var deferred = $q.defer();
deferred.resolve('foo');
return deferred.promise;
}
},
template: '<my-directive></my-directive>'
})
.otherwise('/')
;
}
function myDirective($route) {
return {
link: function(scope, iElement, iAttrs) {
console.log($route.current.locals); // contains data
console.log(scope.$resolve); // undefined
scope.$watch(function() {
return scope.$resolve;
}, function(newValue) {
console.log(newValue); // undefined, never changes
});
}
};
}
最佳答案
此段落位于您正在使用的 1.5 版本的文档中,但不在 1.4.x 版本的文档中。所以这是 1.5 中的新内容。
关于angularjs - Angular ngRoute : Why is $resolve not available on scope in directive link function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957404/