[英]Array length Different in inner function
我做了一個自定義指令。 現在我從chips-break屬性傳遞字符串數據並將其從link 方法轉換為數組。 它工作完美。 但是當我在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打開檢查元素控制台並輸入一些內容並查看差異
當您使用 '@' 時,它會得到一個字符串,這就是為什么您得到 length == 11 的原因,因為您在 '32, 13, 188' 字符串中得到字符數。
查看這篇文章以獲取更多詳細信息AngularJS 中指令范圍內的“@”和“=”有什么區別?
編輯:
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
初始值,它是一個字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.