Javascript 使用AngularFire简化角度控制器到Firebase的发布

Javascript 使用AngularFire简化角度控制器到Firebase的发布,javascript,angularjs,controller,angularfire,Javascript,Angularjs,Controller,Angularfire,我是个新手,但很快就学会了。我有一个可以工作的控制器,基于我黑在一起的演示代码,但是必须有一个更简单更干净的方法来获取所有字段和帖子,所以如果我想添加一个新字段,我不需要一直在不同的位置添加它 这里是控制器: 'use strict'; angular.module('goskirmishApp').controller('addEvent', function ($scope, fbutil, $timeout) { // synchronize a read-only, synchroniz

我是个新手,但很快就学会了。我有一个可以工作的控制器,基于我黑在一起的演示代码,但是必须有一个更简单更干净的方法来获取所有字段和帖子,所以如果我想添加一个新字段,我不需要一直在不同的位置添加它

这里是控制器:

'use strict';
angular.module('goskirmishApp').controller('addEvent', function ($scope, fbutil, $timeout) {
// synchronize a read-only, synchronized array of messages, limit to most recent 10
$scope.messages = fbutil.syncArray('messages', {limit: 10});

// display any errors
$scope.messages.$loaded().catch(alert);

// provide a method for adding a message
$scope.addMessage = function(newEventName,newEventType,newStartDate,newStartTime,newEndDate,newEndTime,newEventDescription,newAddress,newPostcode,newTicketInformation,newBookLink) {
  if( newEventName ) {
    // push a message to the end of the array
    $scope.messages.$add({
        eventName: newEventName,
        eventType: newEventType,
        startDate: newStartDate,
        startTime: newStartTime,
        endDate: newEndDate,
        endTime: newEndTime,
        eventDescription: newEventDescription,
        address: newAddress,
        postcode: newPostcode,
        ticketInformation: newTicketInformation,
        bookLink: newBookLink       
    })
    // display any errors
    .catch(alert);
  }
};

function alert(msg) {
    $scope.err = msg;
    $timeout(function() {
        $scope.err = null;
    }, 5000);
}
});
以及以下观点:

<h2>Add Event</h2>

<p class="alert alert-danger" ng-show="err">{{err}}</p>

<form role="form">
<div class="form-group">
    <label>Event Name</label>
    <input class="form-control" type="text" ng-model="newEventName">
</div>
<div class="form-group">
    <label>Event Type</label>
    <select class="form-control" ng-model="newEventType">
        <option value="" disabled selected>Game type</option>
        <option value="milsim">Skirmish</option>
        <option value="milsim">Special Event</option>
        <option value="milsim">Weekender</option>
        <option value="milsim">Milsim</option>
    </select>
</div>
<div class="form-group">
    <label>Start Date &amp; Time</label>
    <div class="row">
        <div class="col-sm-6">
            <input class="form-control" type="date" placeholder="Date" ng-model="newStartDate"/>
        </div>
        <div class="col-sm-6">
            <input class="form-control" type="time" placeholder="Time" ng-model="newStartTime"/>
        </div>
    </div>
</div>
<div class="form-group">
    <label>End Date &amp; Time</label>
    <div class="row">
        <div class="col-sm-6">
            <input class="form-control" type="date" placeholder="Date" ng-model="newEndDate"/>
        </div>
        <div class="col-sm-6">
            <input class="form-control" type="time" placeholder="Time" ng-model="newEndTime"/>
        </div>
    </div>
</div>
<div class="form-group">
    <label>Event Description</label>
    <textarea class="form-control" rows="4" ng-model="newEventDescription"></textarea>
</div>
<div class="form-group">
    <label>Address</label>
    <input class="form-control" ng-model="newAddress">
</div>
<div class="form-group">
    <label>Postcode</label>
    <input class="form-control" ng-model="newPostcode">
</div>
<div class="form-group">
    <label>Ticket Information</label>
    <textarea class="form-control" rows="4" ng-model="newTicketInformation"></textarea>
</div>
<div class="form-group">
    <label>Booking Link</label>
    <input class="form-control" ng-model="newBookLink">
</div>
<button type="submit" class="btn btn-danger" ng-click="addMessage(newEventName,newEventType,newStartDate,newStartTime,newEndDate,newEndTime,newEventDescription,newAddress,newPostcode,newTicketInformation,newBookLink,newLat,newLong,newApproved);newEventName = null;newEventType = null;newStartDate = null;newStartTime = null;newEndDate = null;newEndTime = null;newEventDescription = null;newAddress = null;newPostcode = null;newTicketInformation = null;newBookLink = null;">Add Event</button>
</form>
添加事件

{{{err}

事件名称 事件类型 游戏类型 小规模战斗 特别活动 周末 米尔西姆 开始日期&;时间 结束日期&;时间 事件描述 地址 邮政编码 票务信息 预订链接 添加事件

非常感谢您的帮助

在将直接发送给firebase的对象中定义所有输入 例如,控制器中的init:

$scope.form = {};
输入模板后,只需将输入定义为“表单属性”

<h2>Add Event</h2>

<p class="alert alert-danger" ng-show="err">{{err}}</p>

<form role="form">
<div class="form-group">
    <label>Event Name</label>
    <input class="form-control" type="text" ng-model="form.eventName">
</div>
<div class="form-group">
    <label>Event Type</label>
    <select class="form-control" ng-model="form.eventType">
        <option value="" disabled selected>Game type</option>
        <option value="milsim">Skirmish</option>
        <option value="milsim">Special Event</option>
        <option value="milsim">Weekender</option>
        <option value="milsim">Milsim</option>
    </select>
</div>
<div class="form-group">
    <label>Start Date &amp; Time</label>
    <div class="row">
        <div class="col-sm-6">
            <input class="form-control" type="date" placeholder="Date" ng-model="form.startDate"/>
        </div>
        <div class="col-sm-6">
            <input class="form-control" type="time" placeholder="Time" ng-model="form.startTime"/>
        </div>
    </div>
</div>
<div class="form-group">
    <label>End Date &amp; Time</label>
    <div class="row">
        <div class="col-sm-6">
            <input class="form-control" type="date" placeholder="Date" ng-model="form.endDate"/>
        </div>
        <div class="col-sm-6">
            <input class="form-control" type="time" placeholder="Time" ng-model="form.endTime"/>
        </div>
    </div>
</div>
<div class="form-group">
    <label>Event Description</label>
    <textarea class="form-control" rows="4" ng-model="form.eventDescription"></textarea>
</div>
<div class="form-group">
    <label>Address</label>
    <input class="form-control" ng-model="form.address">
</div>
<div class="form-group">
    <label>Postcode</label>
    <input class="form-control" ng-model="form.postcode">
</div>
<div class="form-group">
    <label>Ticket Information</label>
    <textarea class="form-control" rows="4" ng-model="form.ticketInformation"></textarea>
</div>
<div class="form-group">
    <label>Booking Link</label>
    <input class="form-control" ng-model="form.bookLink">
</div>
<button type="submit" class="btn btn-danger" ng-click="addMessage()">Add Event</button>
</form>
$scope.addMessage = function() {
  if( $scope.form.eventName ) {
    // push a message to the end of the array
    $scope.messages.$add($scope.form)
    // display any errors
    .catch(alert);

    //Reset your form
    $scope.form = {};
  }
};