提交表单后,我想将该数据推送到我的Firebase数据库,因此我创建了一个功能(addMeeting)。但是,按下按钮提交后我收到错误:
TypeError:undefined不是函数
在l。$ scope.addMeeting(http://localhost:8000/js/controllers/meetings.js:10:12)
meetings.js:10:12是正确的,如果您查看下面的代码我的$ push。
我的HTML:
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Angular Data</title>
<meta name="viewport" content="width=device-width, userscalable=no">
<link rel="stylesheet" href="css/style.css">
<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="js/lib/angular/angular-route.min.js"></script>
<script src="js/lib/angular/angular-animate.min.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.2/firebase.js"></script>
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.0.0/angularfire.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/registration.js"></script>
<script src="js/controllers/meetings.js"></script>
</head>
<body>
<header>
<nav class="cf" ng-include="'views/nav.html'"></nav>
</header>
<div class="page">
<main class="cf" ng-view></main>
</div>
</body>
</html>
我的apps.js:
var myApp = angular.module('myApp',
['ngRoute', 'firebase', 'appControllers']);
var appControllers = angular.module('appControllers', ['firebase']);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/login', {
controller: 'RegistrationController',
templateUrl: 'views/login.html'
}).
when('/register', {
controller: 'RegistrationController',
templateUrl: 'views/register.html'
}).
when('/meetings', {
controller: 'MeetingsController',
templateUrl: 'views/meetings.html'
}).
otherwise({
redirectTo: '/login'
});
}])
meetings.js - 包含失败的addMeeting函数的Controller:
myApp.controller('MeetingsController',
function($scope, $firebaseObject) {
var ref = new Firebase('https://angulardataldc.firebaseio.com/meetings');
var meetings = $firebaseObject(ref);
$scope.meetings = meetings;
$scope.addMeeting = function() {
meetings.$push({
name: $scope.meetingname,
date: Firebase.ServerValue.TIMESTAMP
})
}
}); //MeetingsController
在提交表单时调用该函数的视图:
<section class="meetings cf">
<h1>Add Meetings</h1>
<form class="formgroup addmeeting cf"
name="myform"
ng-submit="addMeeting()"
novalidate>
<div class="inputwrapper">
<input type="text" name="meetingname" placeholder="Meeting Name"
ng-model="meetingname" ng-required="true">
</div>
<button type="submit" class="btn"
ng-disabled="myform.$invalid">+</button>
</form>
<h2>Your Meetings</h2>
<div class="meeting" ng-repeat="meeting in meetings">
<p>{{meeting.name}}</p>
</div>
</section>
**编辑:**它与.push()方法本身有关。我看到在最新版本的angularfire / firebase中它应该是.push,而不是。$ push,ad已经改变了,但它并没有解决我的问题。我将AngularFire和Firebase分别恢复到版本0.8.2和1.0.21,重新引入.asObject()和$ push,一切正常。我不明白为什么.push()失败了所有最新版本(Firebase 2.2.2,AngularFire 1.0)。
答案 0 :(得分:2)
Firebase的AngularFire库有两种主要类型:$firebaseObject
和$firebaseArray
(分别在1.0版本的AngularFire中通过$asObject
和$asArray
实例化。)
您使用了错误的类型和错误的方法。引用AngularFire's documentation on its array type:
同步数组应该用于任何将被排序,迭代和具有唯一ID的对象列表。同步数组假定使用
$add()
添加项目,因此它们将使用Firebase推送ID进行键控。
所以:
var ref = new Firebase('https://angulardataldc.firebaseio.com/meetings');
var meetings = $firebaseArray(ref);
$scope.meetings = meetings;
$scope.addMeeting = function() {
meetings.$add({
name: $scope.meetingname,
date: Firebase.ServerValue.TIMESTAMP
})
}
答案 1 :(得分:0)
您输错了,应该是.push
而不是$push
<强> CODE 强>
$scope.addMeeting = function() {
meetings.push({
name: $scope.meetingname,
date: Firebase.ServerValue.TIMESTAMP
})
}