我是 Angular 新手,正在努力更新 Angular 数组中已从外部更改(不是通过 Angular 支持的 UI)的现有项目。
这是用例...我的网页是通过服务器端调用填充的,我将数组加载到 Angular 中并显示在列表上。
现在,如果服务器上的数据发生变化并且在表中插入了新记录,我的页面的 JavaScript 会收到通知,并通过“推送”成功地将新记录插入到 Angular 数组中(参考文献 1)。在 Angular JS 中以编程方式插入数组值 https://stackoverflow.com/questions/23860072/programmatically-inserting-array-values-in-angular-js).
但是,当现有记录发生更改时(在服务器端/不是通过 Angular 支持的 UI),我的页面也会收到通知。我对如何更新 Angular 数组中的正确记录一无所知? Angular 文档中是否有我错过的查询/更新方法?
这是我当前的代码的样子......
//The HTML UI updates to reflect the new data inserts.
<div ng-repeat="item in items">
<p class="priority">{{item.priority_summary}}</p>
<p class="type">{{item.type}}</p>
</div>
这是脚本...
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"
}];
//The insert works fine. The question is how do I do an update if the notification is for an update, and not for insert.
$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.
$scope.items.push({
status: 'New',
priority_summary: 'H'
});
$scope.$apply();
}
//calling subscribe on controller initialization...
$scope.subscribe();
任何强调这一点的建议或例子都会很棒。谢谢!