[英]Bind a directive's isolated scope to an inner object of the controller's scope
[英]Bind scope to Directive's Controller
请考虑以下角度指令:
angular.module('map').directive('mapOptions',
function MapOptionsDirective() {
return {
restrict: 'E',
transclude: true,
scope: {
map: '=',
layers: '=',
mapId: '@'
},
controller: mapOptionsController,
controllerAs: 'mapOptionsCtrl',
bindToController: true,
templateUrl: 'modules/map/views/mapoptions.client.view.html'
};
///////////////////////////
function mapOptionsController($scope) {
var vm = this;
console.log(vm); // log #1
console.log($scope.map) // log #2
console.log(vm.map); // log #3
}
});
注意隔离的范围, controllerAs
语法以及设置为true
的bindToController
选项。
这是我的指令在html中的外观:
<map-options layers="mapCtrl.layers" map="mapCtrl.map" map-id="{{mapCtrl.mapId}}">
日志输出为:
"constructor {}"
,我认为可以。 undefined
。 在这里,我希望找到在$scope
上找到的对象。 我想将隔离范围直接绑定到控制器,但这不起作用。 奇怪的是,作用域已被数据填充,但是此数据已绑定到作用域,并且并没有直接绑定到指令,正如我希望的那样。 我不知道为什么。 我一定想念一些明显的东西。 谢谢!
我只需要从Angular1.2切换到Angular1.3,现在它就可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.