数组长度内部函数不同

时间:2016-05-05 07:55:51

标签: javascript angularjs angularjs-directive

我制作了一个自定义指令。现在我从 chips-break 属性传递字符串数据,并从链接方法转换为数组。它完美无缺。但是当我在 ng-keydown 方法中获得数组长度时,它会有所不同。请帮我。谢谢高级:

HTML

<input-chips chips-break="32, 13, 188" style="width:80%"></input-chips>

JS

    var app = angular.module("myApp", []);
    app.directive("inputChips", inputChipsFun);

    function inputChipsFun(){
      return{
        restrict : 'EA',
        scope : {
          chipsBreak : "@"
        },
        template: '<div class="chips"><div class="chips-item"></div><input type="text" ng-keydown="inputKeyDown($event, false)"/></div>',
        link : function(scope, elem, attr){
          scope.chipsBreak = scope.chipsBreak.split(",");
          console.log("Length of Chips Break First Time = "+scope.chipsBreak.length);

          scope.inputKeyDown = function($event, is_blur){
            console.log("Length of Chips Break Key Press = " + scope.chipsBreak.length);
          }


        }
      };
    }

请看此链接:https://plnkr.co/edit/RpDwaqjS81DZlZFEzdj2?p=preview 打开检查元素控制台并键入一些内容并查看差异

1 个答案:

答案 0 :(得分:1)

当您使用&#39; @&#39;它会得到一个字符串,这就是为什么你得到长度== 11,因为你得到的字符数为32,31,188和#39;字符串。

查看此帖子了解更多详情 What is the difference between '@' and '=' in directive scope in AngularJS?

编辑:

ArrayList<String> colstring;
colstring = new ArrayList<String>();
for(int i=0;i<facilidades.length();i++)
{
  colstring.add(col.get(i).toString());
}

如果您 link : function(scope, elem, attr){ var x = scope.chipsBreak.split(","); console.log("Length of Chips Break First Time = "+scope.chipsBreak.length); scope.inputKeyDown = function($event, is_blur){ console.log("Length of Chips Break Key Press = " + x.length); } } scope.chipsBreak = scope.chipsBreak.split(",") scope.inputKeyDown(这是一个函数)将获得scope.chipsBreak的初始值,这是一个字符串。