gpt4 book ai didi

javascript - 通知 Angular 一个变量已经改变

转载 作者:行者123 更新时间:2023-11-30 08:01:39 25 4
gpt4 key购买 nike

我刚开始使用 Angular,有一个快速的问题:

var data = JSON.parse('[{"name": "Joe","account": "12355845454","rent": "500"}]');

angular.module('ngAppRentManager', []).
controller('RentCtrl', ['$scope', function ($scope) {
$scope.tenants = data;
}]);

setTimeout(function () {
data = JSON.parse('[{"name": "Sara","account": "54874","rent": "600"}]');
console.log("Poke Angular!");
}, 2000);

当我的变量 data 改变时。我如何告诉 Angular 更新模型?

Fiddle

最佳答案

像这样将 .apply() 添加到您的代码中

var data = JSON.parse('[{"name": "Joe","account": "12355845454","rent": "500"}]');

angular.module('ngAppRentManager', []).
controller('RentCtrl', ['$scope', function ($scope) {
$scope.tenants = data;

setTimeout(function () {
$scope.tenants = JSON.parse('[{"name": "Sara","account": "54874","rent": "600"}]');
console.log("Poke Angular!");
$scope.$apply();
}, 2000);

}]);

http://jsbin.com/tirebavaho/1/

关于javascript - 通知 Angular 一个变量已经改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com