javascript - 范围不更新模型中的更改

标签 javascript django angularjs tastypie

我有一个可扩展的表单,它生成一个具有两个属性的对象:titledescription。该对象作为 json 对象成功提交到我的数据库。我目前正在使用 Angular (1.3.2) 前端,它与 Tastypie 交互,作为与 Django (1.7) 后端的接口(interface)层。问题是,在向数据库添加新对象后,我从未观察到主页的更新。我需要刷新页面才能显示对象,这并不理想。

home.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>

app.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 函数(已注释掉)进行了一些测试,但这要么显示新对象并且永远不会停止(真正删除),要么不更新(但告诉我认为基于控制台语句,数据中有一个额外的对象)(真实存在)。 任何帮助,将不胜感激。

最佳答案

当数据库更新时,前端如何知道它应该获取最新数据,除非我们告诉它?您在服务器和前端之间没有某种套接字,寻找事件并使前端获取最新数据...

因此,当您将数据发布到后端并且数据库已更新时,请在 submit 的成功回调中调用 getProtocols()

在使用 $watch() 的情况下,您会重复从后端获取协议(protocol),这会更新范围变量,从而再次重复触发回调并导致浏览器崩溃。

关于javascript - 范围不更新模型中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27835027/

相关文章:

javascript - 如何仅在 Angular 文件上传中将文件限制为 UTF-8 编码?

javascript - 使用 Gulp 将 jsx 转换为 js 时出现 React 语法错误

c# - session 在 javascript 函数调用后丢失其状态

javascript - 如何在不构建 "audio visualizer"的情况下将 JavaScript 动画与歌曲的节奏同步?

django - 获取 Django 查询集中所有字段的总和

django - 是否可以更改 Django 中的 management/commands 目录?

javascript - 通过从选项 html 中进行选择来打开新选项卡

javascript - 无法从 promise 中捕获异常

Python:检查 Facebook 用户是否已授予访问我的应用程序的权限。 "HTTP Error 400: Bad Request"失败

javascript - 未捕获的语法错误 : missing ) after argument list throwing error in html line