Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 如何将新值附加到Firebase中数组内的项?_Arrays_Angularjs_Angularjs Ng Repeat_Firebase_Angularfire - Fatal编程技术网

Arrays 如何将新值附加到Firebase中数组内的项?

Arrays 如何将新值附加到Firebase中数组内的项?,arrays,angularjs,angularjs-ng-repeat,firebase,angularfire,Arrays,Angularjs,Angularjs Ng Repeat,Firebase,Angularfire,在Firebase中,我有一个“想法”列表。如果用户按下与想法相关联的按钮,我希望在名为“newValue”的属性下为想法添加一个值 例如,下面的html使用ng repeat显示创意数组,并创建一个名为“附加值”的关联按钮。我希望每次用户按下“附加值”时,都将一个新值附加到名为“新值”的创意属性中 <body ng-controller="ctrl"> <table> <tr class="item" ng-repeat="(id,item) in ide

在Firebase中,我有一个“想法”列表。如果用户按下与想法相关联的按钮,我希望在名为“newValue”的属性下为想法添加一个值

例如,下面的html使用ng repeat显示创意数组,并创建一个名为“附加值”的关联按钮。我希望每次用户按下“附加值”时,都将一个新值附加到名为“新值”的创意属性中

<body ng-controller="ctrl">
  <table>
  <tr class="item" ng-repeat="(id,item) in ideas">
    <td>{{item.idea}}</td>
    <td><input ng-model="newValue"></td>
    <td><button ng-click="ValueAppend(id,newValue)">Append Value</button></td>
    </tr>
  </table>
</body>
有关我的工作示例,请参见我的代码笔:

更多说明:
我知道AngularFire提供了$add()和$save()来修改这个数组,但是我如何使用这些方法,以便在数组中的项目下添加一个新的“字符串”。

我不确定这些是否是您的问题,但它们是上面代码和代码笔中的两个错误类型:拼写错误和概念错误

打字错误 您忘记将
$firebase
注入控制器,导致:

“ReferenceError:$firebase未定义”

解决方案当然很简单:

app.controller("ctrl", ["$scope","Ideas", "$firebase",  function($scope,Ideas,$firebase) {
此外,您似乎在
newValue
之前缺少一个斜杠,这意味着您正在尝试创建一个新想法,而不是将该值添加到现有想法中。解决方案同样简单,在
newIdea
之前添加一个斜杠,如下所示:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/" + id + "/newValue";
如果您发现自己更经常犯这种错误,那么通过
child
功能,您可能会成为更好的服务器。虽然它通常是一个多一点的代码,但它不太适合这种打字错误。创建对
newValue
节点的引用将变为:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(id).child("newValue");
概念的 有了这些琐碎的输入错误,我们可以关注真正的问题:如果您将生成的URL记录在console.log中,最容易看到的问题是:

但是,如果您在Firebase forge中查找相同的数据(通过在浏览器中转到),您将看到正确的URL为:

您使用的“0”来自
id
,它是AngularJS数组中想法的索引。但Firebase并不是用
键来实现这个想法的。当AngularFire使用
$asArray
加载数据时,它会将Firebase键映射到角度索引。我们需要执行反向操作将新值写入idea:我们需要将数组索引(在
id
中)映射回Firebase键。为此,您可以调用
[$keyAt(id)][1]
。由于您将想法数组保存在
ideas
中,因此它只是:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(Ideas.$keyAt(id)).child("newValue");
因此,控制器现在变为:

app.controller("ctrl", ["$scope","Ideas", function($scope,Ideas) {
  $scope.ideas = Ideas;
  $scope.idea = "";

  $scope.ValueAppend = function (id,newValue) {   
    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(Ideas.$keyAt(id)).child("newValue");
    var IdeaData = $firebase(IdeaRef);
    $scope.IdeaAttributes = IdeaData.$asArray();
    $scope.IdeaAttributes.$add({
        newValue: newValue,
        timestamp: Date.now()
      });
  };
}]);

我很快在您的代码笔中对其进行了旋转,这似乎起到了作用。

我不确定这些是否是您的问题,但它们是上述代码和代码笔中的两种类型错误:类型错误和概念错误

打字错误 您忘记将
$firebase
注入控制器,导致:

“ReferenceError:$firebase未定义”

解决方案当然很简单:

app.controller("ctrl", ["$scope","Ideas", "$firebase",  function($scope,Ideas,$firebase) {
此外,您似乎在
newValue
之前缺少一个斜杠,这意味着您正在尝试创建一个新想法,而不是将该值添加到现有想法中。解决方案同样简单,在
newIdea
之前添加一个斜杠,如下所示:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/" + id + "/newValue";
如果您发现自己更经常犯这种错误,那么通过
child
功能,您可能会成为更好的服务器。虽然它通常是一个多一点的代码,但它不太适合这种打字错误。创建对
newValue
节点的引用将变为:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(id).child("newValue");
概念的 有了这些琐碎的输入错误,我们可以关注真正的问题:如果您将生成的URL记录在console.log中,最容易看到的问题是:

但是,如果您在Firebase forge中查找相同的数据(通过在浏览器中转到),您将看到正确的URL为:

您使用的“0”来自
id
,它是AngularJS数组中想法的索引。但Firebase并不是用
键来实现这个想法的。当AngularFire使用
$asArray
加载数据时,它会将Firebase键映射到角度索引。我们需要执行反向操作将新值写入idea:我们需要将数组索引(在
id
中)映射回Firebase键。为此,您可以调用
[$keyAt(id)][1]
。由于您将想法数组保存在
ideas
中,因此它只是:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(Ideas.$keyAt(id)).child("newValue");
因此,控制器现在变为:

app.controller("ctrl", ["$scope","Ideas", function($scope,Ideas) {
  $scope.ideas = Ideas;
  $scope.idea = "";

  $scope.ValueAppend = function (id,newValue) {   
    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(Ideas.$keyAt(id)).child("newValue");
    var IdeaData = $firebase(IdeaRef);
    $scope.IdeaAttributes = IdeaData.$asArray();
    $scope.IdeaAttributes.$add({
        newValue: newValue,
        timestamp: Date.now()
      });
  };
}]);

我很快在您的代码笔中对其进行了旋转,这似乎起到了作用。

我不确定这些是否是您的问题,但它们是上述代码和代码笔中的两种类型错误:类型错误和概念错误

打字错误 您忘记将
$firebase
注入控制器,导致:

“ReferenceError:$firebase未定义”

解决方案当然很简单:

app.controller("ctrl", ["$scope","Ideas", "$firebase",  function($scope,Ideas,$firebase) {
此外,您似乎在
newValue
之前缺少一个斜杠,这意味着您正在尝试创建一个新想法,而不是将该值添加到现有想法中。解决方案同样简单,在
newIdea
之前添加一个斜杠,如下所示:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/" + id + "/newValue";
如果您发现自己更经常犯这种错误,那么通过
child
功能,您可能会成为更好的服务器。虽然它通常是一个多一点的代码,但它不太适合这种打字错误。创建对
newValue
节点的引用将变为:

    var URL = "https://crowdfluttr.firebaseio.com/ideas/";
    var IdeaRef = new Firebase(URL).child(id).child("newValue");
概念的 有了这些琐碎的输入错误,我们可以关注真正的问题:如果您将生成的URL记录在console.log中,最容易看到的问题是:

但是,如果您在Firebase forge中查找相同的数据(通过在浏览器中转到),您将看到正确的URL为:

您使用的“0”来自
id
,它是AngularJS数组中想法的索引。但Firebase并不是用
键来实现这个想法的。当AngularFire使用
$asArray
加载数据时,它会将Firebase键映射到角度索引。我们需要执行反向操作将新值写入idea:我们需要映射数组索引(在