简体   繁体   English

多个指令共享相同的作用域

[英]Multiple directives sharing same scope

I have developed a directive, but it is always applying functionality on the last element in the page, here is my code: 我已经开发了一个指令,但是它总是在页面的最后一个元素上应用功能,这是我的代码:

HTML: HTML:

<div ng-controller="MyCtrl">
  <input type="text" email-commy comma-separated-data="model1" class="form-control"/>
  <input type="text" email-commy comma-separated-data="model2" class="form-control"/>
</div>

JS: JS:

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

function MyCtrl($scope) {
    $scope.model1 = "abc@gmail.com";
    $scope.model2 = "abc2@gmail.com";
}

app.directive("emailCommy", function(){
  return {
    scope: {
      commaSeparatedData: '='
    },    
    restrict: "A",  
    multiple: true,
    link: function (scope, elem, attrs, ngModel) {

      function handler(dataString){

        function buildEmailList() {

          var lis = data.map(function (em, index) {
            return "<li>" + em + "<span class='remove-element' data-index='" + index + "'>&times;</span>" + "</li>";
          }).join("");

          var input = $("<input/>", {"class": "form-control shadow-element", "placeholder": "Enter..."});
          lis += "<li class='input-field'>" + input.wrap("<p/>").parent().html() + "</li>";

          return "<ul class='list-inline'>" + lis + "</ul>";
        }

        function renderer() {
          $wrapper.empty().append(buildEmailList());          
          elem.addClass("hide").before($wrapper);
          listener();
        }

        function listener() {
          angular.element(".shadow-element").off("keypress").on("keypress", function (evt) {
            var $this = $(this),
                charCode = evt.which || evt.keyCode;

            //add input in case of (comma, enter, and semi-colon) pressed
            if ($this.val() && (charCode === 44 || charCode === 13 || charCode === 59)) {
              data.push($this.val().toLowerCase());                                    
              renderer();

              updateModel();
              $this.val("");
            }
          });

          angular.element(".remove-element").off("click").on("click", function () {
            var $this = $(this),
                index = $this.data("index");

            data.splice(index, 1);
            updateModel();
            renderer();
          });
        }

        function updateModel(){
          var updatedVal = data.join(",");
          $(elem).val(updatedVal);
        }

        if(dataString) {
          var data = dataString.split(",").map(function (em) {
            return em.toLowerCase().trim();
          });

          var $wrapper = $("<div/>", {"class": "email-container"});

          renderer();
        }
      }

      handler(scope.commaSeparatedData);

      //scope.$watch(attrs.commaSeparatedData, handler);
    }
  };
});

Here is fiddle: http://jsfiddle.net/RmDuw/928/ 这是小提琴: http : //jsfiddle.net/RmDuw/928/

Directive only working on last element. 指令仅在最后一个元素上起作用。 If i add data in first directive it adds to below one. 如果我在第一个指令中添加数据,它将添加到以下一个。

Please help, and suggest me how to fix. 请帮助,并建议我如何解决。 Thanks 谢谢

Just change your code in order to select the input of current directive like this: 只需更改您的代码以选择当前指令的输入,如下所示:

function listener() {
   $wrapper.find('input').on(...

and for remove element: 对于remove元素:

$wrapper.find('span')..

See the fiddle 看到小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM