AngularJS:为什么我的ng模型不会更新?

时间:2014-10-31 11:38:34

标签: angularjs

在这段代码中,我成功地从服务器获得响应,但似乎无法将值显示在我的ng模型中。

我已尝试过各种方式来访问该对象,但认为语法Object.attribute是正确的。我错了吗?

<html ng-app="notesApp">
<head><title>Notes App</title></head>
 <body ng-controller="MainCtrl as ctrl">
<div>
   <h2>Job Inspections?</h2>


<div class="input-group">
    <label>Inspection ID</label>
    <input type="text" ng-model="ctrl.list.id">
</div>
<div class="input-group">
    <label>Inspection ID2</label>
    <input type="text" ng-bind="ctrl.list.id">
</div>
<div class=
<div class="input-group">
    <label>Job ID</label>
    <input type="text" ng-model="ctrl.list.job_id">
</div>
<div class="input-group">
    <label>Inspection Type</label>
    <input type="text" ng-model="ctrl.inspection_type">
</div>
<div class="input-group">
    <label>Bodywork</label>
    <input type="checkbox"
           ng-checked="ctrl.bodywork === 1">
</div>
<div class="input-group">
    <label>Lighting</label>
    <input type="checkbox"
           ng-checked="ctrl.lighting === 'YES'">
</div>

 </div>

<script
  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
<script type="text/javascript">
 angular.module('notesApp', [])
.controller('MainCtrl', ['ItemService', function(ItemService) {
var self = this;
self.list = function() {
  return ItemService.list().then(function(data) {

    console.log(data);
    return data;
    })
}();

//console.log(self.list);
self.add = function() {
  ItemService.add({
    id: self.list().length + 1,
    label: 'Item ' + self.list().length
  });
};

  }])
  .factory('ItemService', ['$http', function($http) {




return {
    list: function() {

        return $http.get('/api_job_inspections/1/edit').then(function(response) {
                    return response.data;
                  }, function(errResponse) {
                    console.error('Error while fetching notes');
                  });

        },
      add: function(item) {
        self.items.push(item);
      }
    };

}]);

</script>
</body>
</html>

按照惯例,提前谢谢你。

1 个答案:

答案 0 :(得分:1)

每次操作数据时都应更新回调函数中的对象:

return ItemService.list().then(function(data) {
    self.list = data;
})

同时确保data符合您的预期。

相关问题