sách gpt4 ai đã đi

javascript - angular ng-click inside ng-repeat 更新 $scope 然后使用 $apply 更新 dom

In lại 作者:行者123 更新时间:2023-11-30 08:02:38 31 4
mua khóa gpt4 Nike

我正在使用 angular 1.2

ng-repeat 创建的 div 也包含 ng-click

点击时 ng-click 更新 $scope

$scope 中的变化反射(reflect)在使用 $apply 的 ng-repeat 中

它有效……但是当我点击时出现错误,我认为我没有正确应用 $apply

Đây là của tôi jsfiddle link

function appcontrol ($scope, $log) {
// declare $scope vars
$scope.currentlist = [];
for (var i = 0; i < 10; i++) {
$scope.currentlist[i] = {'key':i, 'value':i};
}
$scope.extra = 'My Extra';
$scope.anotherextra = 'Another Extra';
// click handler
$scope.handleCellClick = function(cellnumber){
$log.log(cellnumber + ' clicked');
// update the $scope property
$scope.currentlist[cellnumber].value = 'AAA';
// push out the new change to the dom
$scope.$apply();
// trigger other stuff
}
// click handler
$scope.handleExtraClick = function(arg){
$log.log('extra clicked ', arg);
// update the $scope property
if (arg=='My Extra') $scope.extra = 'AAA';
if (arg=='Another Extra') $scope.anotherextra = 'AAA';
// push out the new change to the dom
$scope.$apply();
// trigger other stuff
}
}

和html

 

{{item.value}}

{{extra}}

{{anotherextra}}


1 Câu trả lời

当你使用 ng-click 时,Angular 已经为你调用了 $apply。如果您删除代码中的所有 $scope.$apply(),错误将不会出现,并且会完全一样。 Updated fiddle

关于javascript - angular ng-click inside ng-repeat 更新 $scope 然后使用 $apply 更新 dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252754/

31 4 0
Bài viết được đề xuất: javascript - 如何获取字符串的实际长度?
Bài viết được đề xuất: javascript 属性类型 IE9
Bài viết được đề xuất: java - Ebean 中的部分对象
Bài viết được đề xuất: javascript - 页面上有多个 NVD3 图表。如何简化javascript代码和wrap函数?
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com