【问题标题】:Programmatically inserting array values in Angular JS以编程方式在 Angular JS 中插入数组值
【发布时间】:2014-05-25 21:21:11
【问题描述】:

更新:我能够通过更改调用 push 方法的方式来解决它。请参阅 sn-p 中的内联 cmets。谢谢你的帮助。任何关于为什么这不是一个好主意的 cmets / 想法都将受到高度赞赏。

我有一个绑定到我的 UI 的 Angular JS 数组。当我通过 UI 向它添加一个项目时,它可以正常工作,并且我的 UI 会使用新添加的项目进行更新。所以,这段代码有效...

//The HTML UI based call to addItem works and the UI updates to reflect the new data.
    <table>
      <tr>
        <td>Status: </td>
        <td><input type="text" ng-model="item.status" /></td>
      </tr>
      <tr>
        <td>Priority Summary: </td>
        <td><input type="text" ng-model="item.priority_summary" /></td>
      </tr>
      <tr>
        <td colspan="2"><input type="Button" value="Add" ng-click="addItem(item)" /> </td>
      </tr>
    </table>

<div ng-repeat="item in items">        
    <p class="priority">{{item.priority_summary}}</p>
    <p class="type">{{item.type}}</p>
</div>

这里是 JavaScript

var app = angular.module('DemoApp', []);

<!-- Define controller -->
var contrl = app.controller("MainController", function ($scope) {

$scope.items = [{
    "status": "New",
        "priority_summary": "High"
}, {
    "status": "New",
        "priority_summary": "High"
}, {
    "status": "New",
        "priority_summary": "High"
}, {
    "status": "New",
        "priority_summary": "High"
}];

$scope.addItem = function(item)
{
 alert("addItem called");
 $scope.items.push(item);
 $scope.item = {};
}


  $scope.subscribe = function(){
    //Code for connecting to the endpoint...
    alert("event received"); //We receive this alert, so event is received correctly.

//***This code (items.push(item) is not working 
//and we do not get the UI updated to show the newly added item.
/*Commented - NON WORKING
    item.status = "New";
    item.priority_summary = "High";
    $scope.items.push(item);
   // $scope.$apply();
 });*/

//Working Code....

    $scope.items.push({
      status: 'New',
      priority_summary: 'H'
    });
    $scope.$apply();

    }

  //calling subscribe on controller initialization...
  $scope.subscribe();

但是,我无法理解如何以编程方式添加新项目并在 UI 上查看这些更改。

本质上,代码 sn-p 中的 subscribe() 函数正在侦听外部事件,并且需要以编程方式在列表中插入项目/更新 UI,但不起作用。

我一直在寻找一段时间并尝试了来自 SO 和其他地方的各种示例,但我无法让它适用于这个看起来相当简单的案例。任何指针或指导将不胜感激。

谢谢!

【问题讨论】:

  • 可以添加监听外部事件的代码吗?
  • @AnthonyChu:我添加了订阅功能。由于我对它有各种值的警报,我知道订阅正在工作。我想我搞砸了如何将数组值添加到 Angular 和两种方式绑定以确保它在 UI 上被刷新。
  • $scope.$apply();在您的代码示例中是不必要的
  • itemnewitemsubscribe() 内部指的是什么?
  • 订阅中的新项目似乎未定义

标签: javascript angularjs


【解决方案1】:

在上面的问题中更新了......但我想我应该在这里提到错误代码和工作版本......

//NON WORKING CODE
//***This code (items.push(item) is not working 
//and we do not get the UI updated to show the newly added item.
    item.status = "New";
    item.priority_summary = "High";
    $scope.items.push(item);
   // $scope.$apply();

});

//Working Code....

    $scope.items.push({
      status: 'New',
      priority_summary: 'H'
    });
    $scope.$apply();

【讨论】:

  • 两个版本都应该可以工作(顺便说一句,$scope.$apply 都需要更新 UI - 最好将它包裹在你的代码中,而不是在最后调用它:$scope.$apply(function () { ... });) .但是当然,如​​果我们看不到整个代码,就不可能发现任何问题(例如,item 来自哪里?它是什么样的?等等)。
  • @ExpertSystem:同意。我可能搞砸了我的代码,但否则两者都应该工作。感谢您建议将代码包装在 $scope.$apply(function(){...}) 中,而不是单独调用 apply。我将它合并到代码中。
  • 仅供参考,将代码包装在 $apply() 周围的主要好处是,即使对于 Angular 上下文之外的代码,您也可以使用 Angular 的异常处理。
  • 感谢@ExpertSystem。感谢您分享此提示...我不知道。
【解决方案2】:

不需要将 newItem 从 HTML 传递给控制器​​,因为 newItem 是一个 $scope 变量,并且控制器已经可以访问它。 p>

这是工作代码:

<!-- html -->
<div ng-controller="MyController">
  <table>
    <tr>
      <td>Status: </td>
      <td><input type="text" ng-model="newItem.status" /></td>
    </tr>
    <tr>
      <td>Priority Summary: </td>
      <td><input type="text" ng-model="newItem.priority_summary" /></td>
    </tr>
    <tr>
      <td colspan="2"><input type="Button" value="Add" ng-click="addItem()" /> </td>
    </tr>
  </table>


  <div ng-repeat="item in items">        
    <p class="priority">{{item.priority_summary}}</p>
    <p class="type">{{item.status}}</p>
  </div>
</div>

<!-- js -->
 .controller('MyController', ['$scope', function($scope) {
        $scope.items = [{
                        "status": "New",
                            "priority_summary": "High"
                    }, {
                        "status": "New",
                            "priority_summary": "High"
                    }, {
                        "status": "New",
                            "priority_summary": "High"
                    }, {
                        "status": "New",
                            "priority_summary": "High"
                    }];
        $scope.newItem = {};
        $scope.addItem = function(){
            $scope.items.push($scope.newItem);
            $scope.newItem = {};
        } 

  }]);

【讨论】:

  • 谢谢,但我在上面的代码中遇到了订阅方法的问题。当从 HTML UI 调用时,您上面编写的代码示例对我有用。我的问题是如何从另一个 JS 方法调用中将新数据插入 Angular。
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2014-03-03
  • 2021-11-14
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
相关资源
最近更新 更多