这是我的指令的HTML:
<textarea data-modal="modal" data-mydir ng:model="abc"></textarea>
在我的指令中,我有如下内容:
return {
require: 'ngModel',
replace: true,
scope: {
modal: '=modal',
ngModel: '=',
pid: '=pid'
}
}
谁能告诉我,require:ngModel
的意义是什么?我在许多不同的指令中看到了这一点。我可以称它为数据模式吗?
我很困惑,因为当我把它改为data-modal
时,我从Angular那里得到了一条消息
Controller 'ngModel', required by directive 'textarea', can't be found!
发布于 2014-01-05 14:12:48
require:'ngModel'
和require:'^ngModel'
允许您注入附加到指令绑定到的元素或其父元素的模型。
这基本上是将ngModel传递给链接/编译函数而不是使用作用域选项传递它的最简单方法。一旦你访问了ngModel,你就可以使用$setViewValue
修改它的值,使用$formatters
让它变脏/变干净,应用观察器等等。
下面是一个简单的示例,传递ngModel并在5秒后更改其值。
演示:http://jsfiddle.net/t2GAS/2/
myApp.directive('myDirective', function($timeout) {
return {
restrict: 'EA',
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$render = function() {
$timeout(function() {
ngModel.$setViewValue('StackOverflow');
}, 5000);
};
}
};
});
https://stackoverflow.com/questions/20930592
复制相似问题