【问题标题】:Updating JS Array in angular $scope在角度 $scope 中更新 JS 数组
【发布时间】:2014-07-20 16:43:51
【问题描述】:

我正在尝试更新 $scope 中的数组。举个例子

$scope.data = [{detail:{name: "...", cred:"...."}, date:"current date"},...];
$scope.update = function(){
  $scope.data[0].date = "new date";
}

但是值没有更新。我做错了吗?

【问题讨论】:

  • 你能发布更多你想做的事情吗?这里没有数组。
  • 已编辑问题中的代码。
  • 仍然不清楚您如何调用update 函数以及如何检测到未应用的更新。在视图中显示$scope.data 的用法以及从哪里调用$scope.update
  • $scope.data = [{detail:{name: "...", cred:"...."}, date:"当前日期"},...]; $scope.update = function(){ $scope.data[0].date = "新日期"; console.log($scope.data[0].date); //这会打印当前日期而不是新日期}
  • 检查代码是否有错别字。如果找不到,最好在 Plunker 上说明您的案例

标签: javascript arrays angularjs


【解决方案1】:
$scope.data = [{detail:{name: "...", cred:"...."}, date:"current date"},...];
$scope.update = function(){
  $scope.data[0].date = "new date";
}

您好,有时上述角度视图可能无法直接反映更改,因此要实现这一点,请使用 $watch 或 $watchCollection;

$scope.$watchCollection('data', function(value){
  $scope.data = value;
});

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2015-04-30
    • 2017-02-17
    • 2014-03-24
    • 2016-03-12
    • 2014-06-11
    相关资源
    最近更新 更多