html
<input type="text" name="usernr" ng-model="userNr" placeholder="user nr"
tabindex="2" ng-usernumber/>
javascript
app.directive('ngUserNumber', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
console.log("inside directive");
ctrl.$parsers.push(function(data) {
console.log("//convert data from view format to model format");
data = data.toString() + " test";
return data; //converted
});
ctrl.$formatters.push(function(data) {
console.log("//convert data from model format to view format");
data = data.toString() + " test";
return data; //converted
});
}
};
});
我想用指令内部的数据替换 ng-model 提供的数据。但什么也没有发生。为什么不使用自定义指令?
我关注了这个文档:http
://www.ng-newsletter.com/posts/directives.html
其他来源:https ://stackoverflow.com/a/15346236/489856