范围不更新模型中的更改
Scope not updating changes in the model
我有一个可扩展的形式,它生成一个具有两个属性的对象,一个title
和description
。此对象作为 json 对象成功提交到我的数据库。我目前正在使用一个与Tastypie交互的Angular(1.3.2)前端作为与我的Django(1.7)后端的接口层。问题是在向数据库添加新对象后,我从未观察到主页的更新。我需要刷新页面才能显示不理想的对象。
首页.html
<div class="protocol-list-container">
<div ng-app="protocolApp"
id="protocol-list">
<div class="new-protocol-container" ng-controller="protoCtrl">
<h4>Add New Protocol</h4>
<button type="button"
ng-click="toggle()"
id="id_new">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div ng-hide="visible" class="protocol-new">
<form name="newProtocolForm" novalidate>
<input type="text"
id="id_new_title"
placeholder="Title"
ng-model="protocol.title"
required /><br>
<input type="text"
id="id_new_desc"
placeholder="Description"
ng-model="protocol.description"
required /><br><br>
<input type="submit"
id="id_submit_new_protocol"
value="New Protocol"
ng-click="submit(protocol)"
ng-disabled="newProtocolForm.$invalid">
</form>
{% verbatim %}
<pre>form = {{ protocol | json}}</pre>
{% endverbatim %}
</div>
<div class="protocol">
<h4>My Protocols</h4>
<li ng-repeat="protocol in protocols">
{% verbatim %}
<div><a href="/protocols/{{protocol.id}}"><span ng-bind="protocol.title"></span></a></div>
{% endverbatim %}
<div> - <span ng-bind="protocol.description"></span>
</li>
<br>
</div>
</div>
</div>
应用.js
angular.module('protocolApp', [])
.factory('protocolFactory', ['$http', function($http) {
var urlBase = '/api/v1/protocol/';
var protocolFactory = {};
protocolFactory.getProtocols = function() {
console.log('getProtocols called');
return $http.get(urlBase);
};
protocolFactory.addProtocol = function(protocol) {
console.log('addProtocol called');
return $http.post(urlBase, protocol);
};
return protocolFactory;
}])
.controller('protoCtrl', ['$scope', 'protocolFactory',
function ($scope, protocolFactory) {
$scope.visible = true;
var self = this;
getProtocols();
function getProtocols() {
protocolFactory.getProtocols()
.success(function(data) {
$scope.protocols = data;
})
.error(function(error) {
console.log('error retrieving protocols');
});
}
$scope.toggle = function() {
$scope.visible = !$scope.visible;
var self = this;
var protocol = {};
self.submit = function() {
var protocol = {title: self.title, description: self.description};
console.log('clicked submit with ', self.protocol);
protocolFactory.addProtocol(self.protocol)
.success(function(response) {
console.log('protocol added');
$scope.protocol = null;
})
.error(function(error) {
console.log('post to api failed');
});
// gives the behavior I want, but ultimately crashes chrome
// $scope.$watch('protocols', function(newVal, oldVal) {
// protocolFactory.getProtocols()
// .success(function(data) {
// $scope.protocols = data;
// console.log('watcher data', data);
// });
// }, true);
};
};
}]);
我已经使用 $scope.$watch
函数(注释掉)进行了一些测试,但这要么显示新对象并且永远不会停止(true 已删除)要么不更新(但告诉我数据中有一个额外的对象基于控制台语句)(真实存在)。任何帮助将不胜感激。
当数据库更新时,前端如何知道它应该获取最新数据,除非我们告诉它?你在服务器和前端之间没有某种sockets
,寻找事件并使前端获取最新数据......
因此,当您将数据发布到后端并且数据库已更新时,请在submit
的成功回调中调用getProtocols()
。
在使用 $watch()
的情况下,您反复从后端获取协议,该协议更新了 scope 变量,该变量再次反复触发回调并导致浏览器崩溃。
相关文章:
- Angular没有根据模型更新我的选择元素
- 如何使 NgJsTree 应用模型更新
- 控制器内部的scope函数不根据视图中的ng模型更新值
- Angularjs模型更新延迟
- 模型更新时触发ng显示
- 在特定模型更新后,如何让keystoneJS运行函数
- 挖空JS未从视图模型更新
- 使用Spine.Model.Ajax,如何在模型更新时处理响应的JSON中的额外属性
- 模型更新后,AngularJS 视图未更新
- 角度函数在模型更新之前触发:邮政编码检查器
- 使用 AJAX 更新的模型更新 AngularJS 视图
- 角度指令内的回调在模型更新之前调用
- AngularJS 视图在模型更新后不会更新
- 带有硬编码选项的 AngularJS 多选不会在模型更新时更新
- 角度JS模型更新不起作用
- 角度视图未根据模型更新
- AngularJS-在控制器中使用模型导致模型更新
- Angularjs:当ng模型更新时,选择不更新
- 当使用Knockout's的foreach绑定,do事件会在视图模型更新时自动清理
- 通过ng模型更新不同的$scope对象属性