如何正确使用隔离范围属性?如何正确使用隔离范围属性?
我有一个指令,它是从一个页面控制器调用的,其属性item
传递给它,例如, <my-directive item="myItem"></my-directive>
,包含id
。
下面的代码将不起作用,因为它似乎$scope.item
未被定义在控制器中..就像我太快使用它。我如何确定在我想要使用它时实际设置了它?
app.directive('myDirective', [function() {
return {
restrict: 'E',
templateUrl: 'template.html',
scope: {
item: "="
},
controller: ['$scope', 'ExtendedItemFactory', function($scope, ExtendedItemFactory) {
this.extendedInfo = ExtendedItemFactory.get({ id: $scope.item.id });
}],
controllerAs: 'MyDirectiveCtrl'
};
}]);
使用'$ watch'确实工作! – Andreas
我不得不说,角度不提供更简单的本地解决方案是很奇怪的。 – Andreas
@Andreas是的,它会在没有'.bind(this)'的情况下工作,因为我们正在直接访问isolatated scope变量..如果我们想使用链接的范围变量到指令控制器,那么只有我们会使用'.bind(this) '..如果它确实帮助了你,那就投票赞成..那么.. –