我有一个包裹ngTagsInput与模型拦截/过滤功能角指令
在任何情况下,我的源数据结构是一样的自定义指令,但在一个特定的场景我需要改变保存回数据服务器以动态的方式。
我的标签一般都在格式{id: '123', name: 'Some Text'}
但在我们的数据库中的一个特殊领域,我需要将数据按以下格式{id: 'some-text', name: 'Some Text'}
目前我刚刚更新ng-model
与选定的标签映射。
更新ng-model
我创建了一个Plunkr
例
我之前我想拦截选择的标签,同时改变'123'
ID,以'some-text'
的在解决如何将拦截器添加到角上时遇到问题ř指令
页控制器
app.controller('MainCtrl', function($scope) {
$scope.model = {
skills: [],
desired_skills: []
};
$scope.dashed = function(value) {
var result = _
.words(value)
.map(function(x) { return x.toLowerCase(); })
.join('-');
//console.log(result);
return result;
}
$scope.intercept = function(item) {
console.log('item: ' + JSON.stringify(item));
return { id: $scope.dashed(item.name), name: item.name };
}
})
指令控制器
.controller('SkillTagInputController', ['$scope', '$filter', '$q', function($scope, $filter, $q) {
$scope.loadTags = function (name) {
return [
{id: 5, name: "Some Text"},
{id: 6, name: "More Text"},
{id: 7, name: "Something Else"}
]
};
}])
指令
.directive('wkSkillTagInput', [
function() {
return {
restrict: 'E',
require: '?ngModel',
scope: {
ngModel: '=',
wkOnAddInterceptor: '&'
},
template: '<tags-input \
ng-model="innerModel" \
ng-change="onChange()" \
add-on-paste="true" \
display-property="name" \
add-from-autocomplete-only="true" \
replace-spaces-with-dashes="false"> \
<auto-complete source="loadTags($query)" min-length="2"></auto-complete> \
</tags-input>',
controller: 'SkillTagInputController',
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {
return;
}
// invoked when model changes from the outside
ngModel.$render = function() {
scope.innerModel = ngModel.$modelValue;
};
// invoked when model changes from the inside
scope.onChange = function() {
if (scope.wkOnAddInterceptor) {
// This is the area I need to get working
// The intercepter should get called and change the nagure of the selected item
//ngModel.$setViewValue(scope.$eval(scope.wkOnAddInterceptor));
ngModel.$setViewValue(scope.innerModel);
} else {
ngModel.$setViewValue(scope.innerModel);
}
};
}
};
}
])
谢谢Jesús,教程链接很棒,对你的plunkr有麻烦,但是我已经能够得到我想要的工作版本了:http://plnkr.co /编辑/ q17wGk –
欢迎您。我要检查plunkr来修复错误。谢谢。 –