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

How to use tags-input inside the directive template?

我想在指令模板中使用标签输入。在下面的示例中,我们在指令模板中使用输入文本框,我想使用 tags-input,而不是输入框。请看下面的代码,Inside template of directive 我正在使用 Here Use tags-input: <input type="text" ng-model="modeldisplay" ></input>,我想在这里使用 tag-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;
  }

});

首先确保您正在导入 ngInputTags 脚本:

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

其次,您没有将 ngInputTags 注入到您的模块中。

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

执行这些操作,然后将标记包含在您的模板中: 得到结果。

在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