无法将属性绑定到正确的对象

时间:2015-06-30 18:02:01

标签: angularjs object

我有一个程序列表,每个程序代表数据库中的一个项目(Firebase)。每个项目都由key识别(如底部图片所示)。这个键是我在创建新程序时尝试绑定到program对象的。

但是,我的生命无法将key绑定到传递给program函数的addProgram对象。

完成的程序对象看起来像这样:

program = {
  label: 'test',
  key: '-jWEOkekoktg534944'
}

我现在得到的第一项是这个,让我们假设它的关键是-jWEOkekoktg534944

program = {
  label: 'test'
}

对于第二项:

program = {
  label: 'test',
  key: '-jWEOkekoktg534944' // This is the key that the first item should get, but doesn't
}

所以第三项将获得第二项的关键,依此类推。

输入标记和程序创建按钮:

<div ng-show="actions.add">
  <input type="text" ng-model="program.label" ng-enter="addProgram(program)">
  <span>
    <button ng-click="addProgram(program)">Add</button>
  </span>
</div>

在这里我们得到了控制器,我有一个监视功能以及addProgram函数,它接收program对象。 watch承诺将始终在addProgram承诺(我使用数字控制台日志检查)之前解决,这就像我可以想到为什么这不起作用的最后一个可能原因。然而,我可能会非常错误。

// Watch for changes in the database
// and update our frontend accordingly
programs.$watch(function(event) {

  // Fetch the programs every time it's updated
  programs = fbRefs.getSyncedArray(path);

  // When the data has finished loading
  // update the programs list or throw an error
  programs.$loaded()
    .then(function(res) {

      console.log(1);
      $scope.programs = res;
    })
    .catch(function(err) {
      $scope.addError = 'Unable to create a new program';
  });
});

// Add a new program to firebase
$scope.addProgram = function(program) {

  fbCrud.push(path, program)
    .then(function(ref) {

      // Get the key for the item
      // that was just inserted
      var key = ref.key();

      // And assign it to the program
      // object for later use

      console.log(2);
      program.key = key;
  });
};

结果:

image of outcome

在这里,您可以看到这些项目收到了上一个项目的参考密钥。 使第一个项目没有密钥,因此我将来无法将其删除。

为什么这不起作用呢?

0 个答案:

没有答案
相关问题