1

我尝试让firebase绑定与knockoutjs一起工作,但无法让它们工作,所以我正在尝试用angularjs进行一些测试。所以我下载了 angularjs,我试图输入/输出一些数据。所以现在我的 app.js 中有这个:

/* Controllers */

angular.module('MyCtrl3', ['$scope', 'angularFire'])
.controller ("MyCtrl3", function ($scope, angularFire) {
$scope.addMsg = function ($scope, angularFire) {
    var url = 'https://kingpinapp.firebaseio.com/msgs';
    var promise = angularFire(url, $scope, 'msgs', []);
    $scope.msgs.push({name: "Firebase", desc: "is awesome!"});

}
});

然后我的html就是:

<!doctype html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>

<div>Angular seed app: v<span app-version></span></div>

<div ng-controller="MyCtrl3">

<button ng-click="addMsg()">Add</button>
</div>

<!-- bunch of scripts -->
</body>
</html>

只是试图让添加按钮推送到firebase。我在控制台中收到此错误:Uncaught TypeError: Cannot call method 'push' of undefined。添加了一个jsfiddle:http: //jsfiddle.net/NHaZz/5/

4

2 回答 2

3

AngularFire 返回一个promise,因此在promise 完成之前你不能操作数组。您可以使用该then函数执行此操作,如下所示:

angular.module('MyCtrl3', ['$scope', 'angularFire'])
.controller ("MyCtrl3", function ($scope, angularFire) {
  var url = 'https://kingpinapp.firebaseio.com/msgs';
  var promise = angularFire(url, $scope, 'msgs', []);
  promise.then(function() {
    $scope.addMsg = function ($scope, angularFire) {
      $scope.msgs.push({name: "Firebase", desc: "is awesome!"});
    }
  });
});
于 2013-04-29T18:26:07.247 回答
2

AngularFire 0.3.0 似乎发生了一些变化,所以你不能只使用 angularFire 发送一个 url,你需要提供:

new Firebase("example-url-to-your-firebase.com");

像这样:http: //jsfiddle.net/DR4r9/14/

于 2013-11-24T16:04:01.487 回答