【问题标题】:Angular js-Web page freezes after form submitted提交表单后,Angular js-Web页面冻结
【发布时间】:2017-08-02 20:23:27
【问题描述】:

这是我正在做的一个学习 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>

【问题讨论】:

    标签: html angularjs firebase firebase-realtime-database


    【解决方案1】:

    在检查您的代码时,我感觉您可能没有初始化 firebase 模块。

    所以你应该先做

    配置 firebase.initializeApp() 否则不起作用

      .config(function () {
            var config = {
                  apiKey: "",
                  authDomain: "",
                  databaseURL: "",
                  storageBucket: "",
                  messagingSenderId: ""
                };
            firebase.initializeApp(config);
        });
    

    查看此示例,请使用您自己的 firebase 初始化信息使其工作我让您的代码在 muy 测试中工作。

    示例:https://jsfiddle.net/moplin/zLozccap/

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 2012-05-25
      • 1970-01-01
      相关资源
      最近更新 更多