1
似乎無法獲得角格式化和解析工作:採用了棱角分明的格式化/解析器
app.js:
var itemApp = angular.module('itemApp', []);
itemApp.controller('ItemController', ['$scope', function($scope) {
$scope.some_value = 'abcEFG';
}]);
itemApp.directive('changeCase', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
ngModel.$formatters.push(function(value){
return value.toUpperCase();
});
ngModel.$parsers.push(function(value){
return value.toLowerCase();
});
}
};
});
view.html:
<input ng-model="some_value" changeCase>
它不起作用 - 沒有錯誤,只是輸入或模型中的值沒有任何反應。我對Angular很陌生,無法弄清楚我甚至會如何調試。
[toUpperCase](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase)和[toLowerCase](HTTPS://顯影劑。 mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase),並且所有字符串操作方法都不會操作字符串,它們會返回轉換後的字符串。所以你會'返回value.toUpperCase()'等 –
對不起,草率的示例代碼。仍然不起作用,雖然 – Yarin