如果你只是想從鍵入逗號停止用戶,你可以做到這一點沒有jQuery的:
$html.on('keydown', 'li', function (e) {
if (e.keyCode === 188) {
return false;
}
});
還有(只要我能用有限的上下文來講)沒有理由進行$編譯。您應該能夠只使用一個模板:
http://jsfiddle.net/VM285/
myApp.directive('mydirective', function() {
return {
restrict: 'E',
replace: true,
scope: {
list: '='
},
link: function (scope, element, attrs) {
element.bind('keydown', function (e) {
if (e.keyCode === 188) {
e.preventDefault();
return false;
}
});
},
template: '<div><ul ng-model="list"><li ng-repeat="item in list"><input ng-model="item.Name" type="text" /></li></ul></div>'
}
});
服用後1頭更進一步,你可以這樣做完全沒有DOM操作:
http://jsfiddle.net/VM285/1/
myApp.directive('mydirective', function() {
return {
restrict: 'E',
replace: true,
scope: {
list: '='
},
link: function (scope, element, attrs) {
scope.checkForComma = function(item) {
if (item.Name.indexOf(',') > -1) {
item.Name = item.Name.replace(/,/g, '');
}
};
},
template: '<div><ul ng-model="list"><li ng-repeat="item in list"><input ng-model="item.Name" type="text" ng-change="checkForComma(item)" /></li></ul></div>'
}
});
? – 2013-04-30 18:33:23
當輸入失去焦點時,逗號被刪除..我不明白你的問題。 – lucuma 2013-04-30 19:14:54