ngModel 通过包装器传递给子指令
ngModel passed through wrapper to child directive
让我们想象一下,我们有一个子指令 <child/>
,它接受 ng-model
和 ng-change
并对它们执行一些操作。我们有两种包装器 <w1/>
和 <w2/>
,它们包含 <child/>
.
- W1 应该将 ng-model 直接传递给
<child/>
- W2 应该将一些内部对象作为模型传递给
<child/>
在第一种情况下我会使用 require: '^ngModel'
第二:require: 'ngModel'
但我需要他们同时工作
所以模型很简单object可以轻松通过。
<wrapper ng-model="foo"></wrapper>
包装器:
module
.directive('wrapper', function () {
return {
restrict: 'E',
template: "<child ng-model='ngModel'></child>",
scope: {
ngModel:'='
},
link: function ($scope) {
}
};
});
Child:
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;
}
};
});
让我们想象一下,我们有一个子指令 <child/>
,它接受 ng-model
和 ng-change
并对它们执行一些操作。我们有两种包装器 <w1/>
和 <w2/>
,它们包含 <child/>
.
- W1 应该将 ng-model 直接传递给
<child/>
- W2 应该将一些内部对象作为模型传递给
<child/>
在第一种情况下我会使用 require: '^ngModel'
第二:require: 'ngModel'
但我需要他们同时工作
所以模型很简单object可以轻松通过。
<wrapper ng-model="foo"></wrapper>
包装器:
module
.directive('wrapper', function () {
return {
restrict: 'E',
template: "<child ng-model='ngModel'></child>",
scope: {
ngModel:'='
},
link: function ($scope) {
}
};
});
Child:
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;
}
};
});