gpt4 book ai didi

javascript - 如何在指令模板中使用标签输入?

转载 作者:行者123 更新时间:2023-11-27 22:58:21 27 4
gpt4 key购买 nike

我想在指令模板内使用标签输入。在下面的示例中,我们在指令模板内使用输入文本框,我想使用 tags-input ,而不是输入框。请参阅以下代码,指令模板内部我正在使用Here Use tags-input: <input type="text" ng-model="modeldisplay" ></input> ,我想在这里使用标签输入:

为此,请包括以下库

 <script src="http://mbenford.github.io/ngTagsInput/js/ng-tags-input.min.js"></script>

Plunker Demo:

// Code goes here 
var app = angular.module("myApp", ['ngTagsInput']);

app.directive("myDirective", function(){
return {
restrict: "E",
template : '<h1>Click to choose!</h1><div class="clkm"'+
'ng-repeat="item in items" ng-click="updateModel(item)">{{item}}</div>' +
'Here Use tag-input: <input type="text" ng-model="modeldisplay" ></input>',
require: 'ngModel',
scope : {
items : "=",
modeldisplay:'= modeldisplay'
},
link : function(scope, element, attrs, ctrl){
scope.updateModel = function(item)
{
ctrl.$setViewValue(item);
scope.modeldisplay = item;
}
}
};
});

app.controller("appCtrl", function($scope){
$scope.items = [1,2,3,4,5,6];
$scope.bar = function(foo) {
$scope.aux = foo;
}

});

最佳答案

在html中添加css和js:

<link rel="stylesheet" href="http://mbenford.github.io/ngTagsInput/css/ng-tags-input.min.css" />
<script src="http://mbenford.github.io/ngTagsInput/js/ng-tags-input.min.js"></script>

对代码进行以下更改:
1. 将 scope.modeldisplay = item; 替换为 scope.modeldisplay.push({"text":item});

// Code goes here
var app = angular.module("myApp", ['ngTagsInput']);

app.directive("myDirective", function(){
return {
restrict: "E",
template : '<h1>Click to choose!</h1><div class="clkm"'+
'ng-repeat="item in items" ng-click="updateModel(item)">{{item}}</div>' +
'Here Use tag-input: <tags-input ng-model="modeldisplay" ></tags-input>',
require: 'ngModel',
scope : {
items : "=",
modeldisplay: "="
},
link : function(scope, element, attrs, ctrl){
scope.updateModel = function(item)
{
ctrl.$setViewValue(item);
scope.modeldisplay.push({"text":item});
}
}
};
});

app.controller("appCtrl", function($scope){
$scope.items = [1,2,3,4,5,6];
$scope.tags = [];
$scope.bar = function(foo) {
$scope.aux = foo;
};

});

Demo

关于javascript - 如何在指令模板中使用标签输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37376371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com