这是我学习Angular JS的一个项目。它是一个单页Web应用程序,允许记录联系人并对其进行编辑。出于某种原因,我无法弄清楚,每当我将数据发布到Firebase时,我的网页都会冻结。任何人都可以帮忙吗?
这是我的javascript文件
'use strict';
angular.module('myContacts.contacts', ['ngRoute','firebase'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/contacts', {
templateUrl: 'contacts/contacts.html',
controller: 'contactsCtrl'
});
}])
.controller('contactsCtrl', ['$scope','$firebaseArray', function($scope, $firebaseArray) {
var ref = firebase.database().ref();
$scope.contacts = $firebaseArray(ref);
//console.log($scope.contacts);
$scope.showAddForm = function(){
$scope.addFormShow = true;
}
$scope.hide = function(){
$scope.addFormShow = false;
}
$scope.addFormSubmit = function(){
console.log('adding contact');
//Assign values
if($scope.name){var name = $scope.name}else {name=null;}
if($scope.email){var email = $scope.email}else {email=null;}
if($scope.company){var company = $scope.company}else {company=null;}
if($scope.mobile_phone){var mobile_phone = $scope.mobile_phone}else { mobile_phone=null;}
if($scope.home_phone){var home_phone = $scope.home_phone}else {home_phone=null;}
if($scope.work_phone){var work_phone = $scope.work_phone}else {work_phone=null;}
if($scope.street_address){var street_address = $scope.street_address}else {street_address=null;}
if($scope.city){var city = $scope.city}else {city=null;}
if($scope.province){var province = $scope.province}else {province=null;}
if($scope.postal_code){var postal_code = $scope.postal_code}else {postal_code=null;}
//Build Object
$scope.contacts.$add({
name:name,
email:email,
company:company,
phone:[
{
mobile:mobile_phone,
home:home_phone,
work:work_phone
}
],
address:[
{
street_address:street_address,
city: city,
province: province,
postal_code: postal_code
}
]
}).then(function(ref){
var id = ref.key();
console.log('added contact with id: '+ id);
//clear form
clearFields();
hide form
$scope.addFormShow = false;
//send message
$scope.msg ="contact Added";
});
}
//Clear $scope Fields
function clearFields(){
console.log('Clearing All Fields');
$scope.name = '';
$scope.email = '';
$scope.company = '';
$scope.mobile_phone = '';
$scope.home_phone = '';
$scope.work_phone = '';
$scope.street_address = '';
$scope.city = '';
$scope.province = '';
$scope.postal_code = '';
}
}]);

这是HTML文件
<div class="row" ng-controller="contactsCtrl">
<div class="large-10 columns">
<!--<div data-alert ng-show="msg" class="alert-box">{{msg}}</div>-->
<form ng-submit="addFormSubmit()" ng-show="addFormShow">
<h3>Add Contact</h3>
<!--Add Form-->
<div class="row">
<div class="large-6 columns">
<label>Name:
<input type="text" ng-model="name" placeholder="Contact Name" required/>
</label>
</div>
<div class="large-6 columns">
<label>Email:
<input type="text" ng-model="email" placeholder="Contact Email" required/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Company:
<input type="text" ng-model="company" placeholder="Company Name"/>
</label>
</div>
<div class="large-6 columns">
<label>Work Phone:
<input type="text" ng-model="work_phone" placeholder="Work Phone"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Mobile Phone:
<input type="text" ng-model="mobile_phone" placeholder="Mobile Phone" />
</label>
</div>
<div class="large-6 columns">
<label>Home Phone:
<input type="text" ng-model="home_phone" placeholder="Mobile Phone"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Street Address:
<input type="text" ng-model="street_address" placeholder="Street Name"/>
</label>
</div>
<div class="large-6 columns">
<label>City:
<input type="text" ng-model="city" placeholder="City"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Province:
<input type="text" ng-model="state" placeholder="Province"/>
</label>
</div>
<div class="large-6 columns">
<label>Postal Code:
<input type="text" ng-model="postal_code" placeholder="Postal Code"/>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<input type="submit" value="Add Contact" class="button"/>
</div>
</div>
</form>
<h3>Your Contacts (3)</h3>
<table>
<thead>
<tr>
<th width="200px">Name</th>
<th width="200px">Company</th>
<th width="25%">Email</th>
<th width="25%">Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td><a href="#">{{contact.name}}</a></td>
<td>{{contact.company}}</td>
<td>{{contact.email}}</td>
<td><a class="button tiny" ng-click="showEditForm(contact)">Edit</a><a class="button tiny alert" ng-click="removeContact(contact)">Delete</a></td>
</tr>
</tbody>
</table>
</div>
<div class="small-12 large-2 columns">
<a class="button large" ng-click="showAddForm()" ng-hide="addFormShow">+</a>
<a class="button large" ng-click="hide()" ng-show="addFormShow">-</a>
</div>
</div>
&#13;
答案 0 :(得分:0)
在检查代码时,我觉得您可能没有初始化firebase模块。
所以你应该先做
配置firebase.initializeApp()或它无法正常工作
.config(function () {
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
});
检查此示例,请使用您自己的firebase初始化信息使其工作我使您的代码在muy测试中工作。
示例:https://www.microsoft.com/en-us/download/details.aspx?id=30683