0
テキストフィールドにタグを追加できません。テキストフィールドにテキストをタグ付けするアプローチを教えてもらえますか?タグをjsonオブジェクトまたは変数にバインドする方法。angleJSを使用したタグ付けのカスタムディレクティブ
</script>
<script>
var app = angular.module('myApp',[]);
app.directive('tagInputType', function() {
return {
restrict: 'E',
scope: { tags: '=' },
template:
'<div class="tags">' +
'<a ng-repeat="(idx, tag) in tags" class="tag" ng-click="remove(idx)">{{tag}}</a>' +
'</div>' +
'<input type="text" placeholder="Add a tag..." ng-model="new_value"></input> ' +
'<input type="button" value="Add" ng-click="add()"></input>',
link: function ($scope, $element) {
// var input = angular.element($element.children()[1]);
$scope.add = function() {
$scope.tags.push($scope.new_value);
$scope.new_value = "";
};
$scope.remove = function (idx) {
$scope.tags.splice(idx, 1);
};
input.bind('keypress', function (event) {
if (event.keyCode == 13) {
$scope.$apply($scope.add);
}
});
}
};
});
app.controller('MainCtrl', function ($scope) {
$scope.tags = { "value1":"angular","value2":"html"};
});
</script>