作爲AngularJs中的新手無法理解問題。AngularJs。在指令中更改模型太慢
這是一個顯示動作的簡單示例。實際上約50個項目,點擊「沒有字符串」後點擊元素「字符串#」以視覺上刪除該項目大約需要1-2秒。
我的控制器代碼:
testApp.controller('TestController', ['$scope', function ($scope) {
$scope.category = [
{id:1, name: "Category 1"},
...
];
$scope.items = [
{id: 1, category: {id: 1},name: "Test 1"},
...
];
$scope.list = [
{id: 1,name: "String 1"}
...
];
angular.forEach($scope.category, function(categoryItem, i) {
categoryHash[categoryItem.id] = i;
});
angular.forEach(menuItems, function(item) {
var catCategory = categoryHash[item.category.id];
if (!$scope.category[catCategory].items) {
$scope.category[catCategory].items = [];
}
$scope.category[catCategory].items.push(item);
});
}])
指令代碼:
.directive('listItems', function() {
return {
restrict: 'E',
scope: {
listArray: '=',
listItemId: '=',
listFlag: '='
},
template: '<ul>' +
'<li ng-repeat="listStr in listArray track by listStr.id">' +
'<input type="radio" ' +
'id="list_{{ listItemId }}_{{ listStr.id }}" name="list_{{ listItemId }}" ' +
'ng-model="$parent.$parent.item.string" ng-value="listStr" ng-change="stringSelect()">' +
'<label for="list_{{ listItemId }}_{{ listStr.id }}" ng-bind="listStr.name"></label>' +
'</li>' +
'<li>' +
'<input type="radio" ' +
'id="list_{{ listItemId }}_0" name="list_{{ listItemId }}" ' +
'ng-model="$parent.$parent.item.string" ng-value="" ng-change="stringSelect()">' +
'<label for="list_{{ listItemId }}_0">Without string</label>' +
'</li>' +
'</ul>',
link: function(scope, iElement, iAttrs) {
scope.stringSelect = function() {
scope.listFlag = false;
};
}
}
})
模板:
<div ng-app="test" ng-controller="TestController">
<div ng-repeat="collection in category track by $index" >
<h3 ng-bind="collection.name"></h3>
<ul>
<li ng-repeat="item in collection.items track by $index">
<strong ng-bind="item.name"></strong>
<span ng-if="item.string" ng-bind="item.string.name"></span>
<button ng-click="addString = true" ng-hide="addString">Add String</button>
<div ng-if="addString">
<list-items
list-array="list"
list-item-id="$parent.item.id"
list-flag="$parent.addString"></list-items>
</div>
</li>
</ul>
</div>
</div>
它在搗鼓我很即時。 – DoctorMick