【发布时间】:2015-06-20 09:24:14
【问题描述】:
我正在尝试对我的输入元素进行过滤。 我想对 type="text" 字段的输入进行过滤。 例如,如果模型包含的可用字符多于我想要更改的输入值。 我创建了jsfiddle 我有动态生成 html 模板的指令,它包含输入字段。
var app = angular.module('app', [])
.controller('ctrlr', function($scope){
$scope.myModel = "test";
$scope.availableCharacters = 5;
$scope.$watch('myModel', function(newValue, oldValue){
if(!newValue){
return;
}
if(newValue.length > 5){
$scope.cutString();
}
});
$scope.cutString = function(){
var length = $scope.myModel.length;
var string = $scope.myModel.slice(0, $scope.availableCharacters);
var countStars = length - string.length;
$scope.myModel = $scope.createStars(string, countStars);
}
$scope.createStars = function(string, countStars){
for(var i = 1; i <= countStars; i++){
string = string+'*';
}
return string;
}
})
.directive('awesome' , function(){
return {
restrict:'E',
template:'<input type="text" ng-model="myModel" ng-value="myModel | filter:my" />'
}
})
是否可以将我的代码移动到过滤器函数中?我有很多业务逻辑,我不想将我的代码保留在控制器中,因为它将是可重用的指令。
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-filter