在使用ngRoute的Angular v1.4.8应用程序中,我有一个解析的路由,我想在自定义指令的链接函数中访问该路由。
根据文档:
为了更轻松地从模板访问已解析的依赖项, 解析地图将在路线
scope
下提供$resolve
(默认情况下)或resolveAs
指定的自定义名称 财产。
。然而,在我的自定义指令的链接函数中的 scope
属性上未定义$resolve
- 即使我观察它的变化也是如此。
为什么$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 中的新内容。