2
在我使用ngRoute的Angular v1.4.8应用程序中,我有一个想要在自定义指令的链接函数中访问的解决方案。
根据文档:
对于从模板解析的依赖更容易获得,在 决心地图将可在路由的
scope
,下$resolve
(默认)或自定义名称由resolveAs
属性指定。
...但$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.0-rc.0):https://github.com/angular/angular.js/pull/13400 –
我猜这有助于阅读手册的正确版本。谢谢! –