是否可以在Directive中访问链接到Controller的ng-model?
<input type="text" ng-model="ctrl.valuelist.value" />
控制器:
ctrl.valuelist.value = 'initial value';
指令:
function Directive($window) {
return {
restrict: 'E',
scope: {
},
controller: 'ctrl as ctrl',
link: function(scope, elt, attrs){
// something like this:
scope.valuelist.value = 'New value';
}
};
}
答案 0 :(得分:0)
function Directive($window) {
return {
restrict: 'E',
scope: {
ngModel: '=' //<----- access to the model by this
},
controller: 'ctrl as ctrl',
link: function(scope, elt, attrs){
// something like this:
scope.ngModel = 'New value';
}
};
}
我是一个傻瓜,所以要善待。
答案 1 :(得分:0)
您可以通过双向('=')将属性传递给您的指令,这意味着当您从指令进行更改时也会反映在控制器中
function Directive($window) {
return {
restrict: 'E',
scope: {
list: '='
},
controller: 'ctrl as ctrl',
link: function(scope, elt, attrs){
// something like this:
scope.list.value = 'New value';
}
};
}
您必须将 valuelist 传递给您的指令示例:
<directive list='valuelist'></directive>