是否可以在javascript中声明ng-repeat
个元素。
我用谷歌搜索试图找到答案,但我找不到。如果有其他方式,请提出一些我现在真的迷失方向的想法。
谢谢
HTML
<div class="" ng-app='app' ng-controller='appCtrl'>
<div ng-repeat='el in names'>
<div ng-click="click">{{el}}</div>
</div>
</div>
的JavaScript
var app = angular.module('app', [])
var appCtrl = function ($scope) {
$scope.names = [{
name: 'jane'
}, {
name1: 'jane1',
name2: 'jane2'
}, {
name3: 'jane3'
}];
$scope.click = {
// I want to declare this part
//
//
// if (el.length() == 2) {
// alert('true')
// }
}
}
app.controller('appCtrl', appCtrl())
答案 0 :(得分:0)
Check this answer
我使用input
标签进行演示。
<div class="" ng-app='app' ng-controller='appCtrl'>
<div ng-repeat='el in names'>
<input id="{{el.id}}" ng-click="clickz(el.id)" value = "{{el}}" > {{el}}</input>
</div>
</div>
功能clickz
。如果names
的数量等于2,则会发出警报。
$scope.clickz = function(id){
//alert(id);
var len = document.getElementById(id).value.split(",").length-1;
if(len == 2 ){
alert(true)
}
}