【问题标题】:ngModel passed through wrapper to child directivengModel 通过包装器传递给子指令
【发布时间】:2015-06-30 09:57:27
【问题描述】:
让我们想象一下,我们有一个子指令<child/>,它接受ng-model 和ng-change 并对它们执行一些操作。我们有两种包装器<w1/> 和<w2/>,它们包含<child/>。
- W1 应该将 ng-model 直接传递给
<child/>
- W2 应该将一些内部对象作为模型传递给
<child/>
在第一种情况下,我会使用require: '^ngModel'
第二个:require: 'ngModel' 但我需要他们同时工作
【问题讨论】:
标签:
angularjs
angularjs-directive
angular-ngmodel
【解决方案1】:
所以模型是可以轻松传递的简单对象。
<wrapper ng-model="foo"></wrapper>
包装器:
module
.directive('wrapper', function () {
return {
restrict: 'E',
template: "<child ng-model='ngModel'></child>",
scope: {
ngModel:'='
},
link: function ($scope) {
}
};
});
孩子:
module
.directive('child', function () {
return {
restrict: 'E',
require: 'ngModel',
template: "<div>some wierd stuff</div>",
scope: {
},
link: function ($scope, iElem, iAttr, ngModel) {
var a = ngModel.$viewValue;
}
};
});