我想调用一个角度指令。这应该要求每次都在所有事件eg:- ng-click..etc
之前。当视图值发生变化时,它应该是查看chaged时的第一个调用。
以下是fiddle.
angular.module('myApp', []);
angular
.module('myApp').directive('format', ['$filter', function ($filter) {
return {
require: 'ngModel',
scope: {
val : '=val'
},
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
if(attrs.symbol == '$')
return $filter(attrs.format)(ctrl.$modelValue, '$')
else
return $filter(attrs.format)(ctrl.$modelValue)
});
elem.bind('change', function(event) {
var a = elem.val();
var plainNumber = a.split('.').filter(function(e){
return (e.length > 0);
}).join('.');
var i = 0;
if(isNaN(parseFloat(plainNumber))){
i = (attrs.symbol == '$') ? 1 : 3;
}
var num = plainNumber.substring(i, plainNumber.length).replace(/,/g,'');
if(attrs.symbol == '$')
elem.val($filter('currency')(num, attrs.symbol));
else
elem.val($filter('currency')(num));
var n =parseFloat(num);
scope.val = Number(n);
if(!scope.$$phase) {
scope.$apply();
}
});
}
};
}]);
我怎样才能每次调用指令。
答案 0 :(得分:0)