不确定如何表达问题所以请编辑,如果你能想出更好的东西.我有以下指令:
app.directive('foo', function() {
return {
restrict: 'A',
require: "?ngModel",
link: function (scope, element, attrs, controller) {
scope.$watch(attrs.ngModel, function () {
console.log("Changed to " + scope[attrs.ngModel]);
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
当我有这个它很好,并正确记录
<input type="text" ng-model="bar" />
app.controller('fooController', function($scope) {
$scope.bar = 'ice cream';
});
Run Code Online (Sandbox Code Playgroud)
当我以这种方式尝试它时,它不起作用.它不断记录'Changed to undefined'
<input type="text" ng-model="model.bar" />
app.controller('fooController', function($scope) {
$scope.model = { bar: 'ice cream' };
});
Run Code Online (Sandbox Code Playgroud)
如何使它适用于这两种情况.看起来正确的角度让你可以使用两者.
uri*_*ium 11
我查看了ngModel指令,发现了一个名为ngModelGet的函数.使用$ parse.
app.directive('foo', function($parse) {
return {
restrict: 'A',
require: "?ngModel",
link: function (scope, element, attrs, controller) {
var ngModelGet = $parse(attrs.ngModel);
scope.$watch(attrs.ngModel, function () {
console.log("Changed to " + ngModelGet(scope));
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以使用
var ngModelCtrl = controller;
ngModelCtrl.$viewValue
Run Code Online (Sandbox Code Playgroud)
更换
scope[attrs.ngModel]
Run Code Online (Sandbox Code Playgroud)
这是ngModelCtrl sdk
| 归档时间: |
|
| 查看次数: |
15307 次 |
| 最近记录: |