Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用firestore中的数据更新angularjs ng repeat_Angularjs_Google Cloud Firestore - Fatal编程技术网

使用firestore中的数据更新angularjs ng repeat

使用firestore中的数据更新angularjs ng repeat,angularjs,google-cloud-firestore,Angularjs,Google Cloud Firestore,Html 你能试试这个吗? 我猜你在这里失去了数组引用 <li ng-repeat="name in $ctrl.names">{{name}}</li> 找到了解决办法。将我要更新的变量包装在$scope.$apply()中。这告诉angularjs注意$scope.name上的更新,之前没有这样做,因为$scope.names变量是在angularjs上下文之外更新的 let names = []; $ctrl = this; $ctrl.names = names

Html

你能试试这个吗? 我猜你在这里失去了数组引用

<li ng-repeat="name in $ctrl.names">{{name}}</li>

找到了解决办法。将我要更新的变量包装在$scope.$apply()中。这告诉angularjs注意$scope.name上的更新,之前没有这样做,因为$scope.names变量是在angularjs上下文之外更新的

let names = [];
$ctrl = this;
$ctrl.names = names;
listRef.get()
.then(function(doc) {
  if (doc.exists) {
    $ctrl.names = doc.data().listOfNames;
  }
}

names=doc.data().listOfNames
应该是
this.names=doc.data().listOfNames感谢您的反馈,但是在函数中写入此代码。按照您的建议,函数中的名称返回未定义。您可以共享一些代码吗。我认为您在某种程度上缺少了
promise
,我共享的代码出现在我的angularjs控制器函数中。因为我使用的是then方法,所以我认为我不需要使用promise,但我可能弄错了。您知道如何使用promise以某种方式使用firestore中的数据更新ng repeat吗?如果是这样,我想举个简单的例子。谢谢。@EFlynn当您将一些模拟数据传递给
名称
数组时,您是否检查了您的
li
是否得到更新?另外,您是否将控制器用作$ctrl?在
内部,然后
控制台输出$ctrl.name是什么?作为一种可能的解决方案,我想尝试使用$http而不是firebase get()方法来检索数据。然而,我正在努力实现这种方法
$scope.names = [];
listRef.get()
.then(function(doc) {
  if (doc.exists) {
    $scope.$apply(function () {
      $scope.names = doc.data().listOfNames;
    }
  }
}