将Angular Form保存为JSON

时间:2017-08-21 21:18:49

标签: angularjs json angular-material

我有一个Angular表单,我希望将数据保存在表单创建实例中,并在每次提交时保存数据。我对表单进行了编码,并查看了创建控制器和工厂的帮助,但是我无法掌握概念并实现工作版本。

目标是我可以在必要时回复此数据。感谢帮助。

HTML

    <div>
        <form name="eventForm" ng-submit="addEvent()" ng-model="eventHere">
            <div ng-hide="eventFinish()">
                <h2>First Step</h2>

                <md-input-container layout-fill flex>
                    <md-select name="type" ng-model="eve.id" placeholder="Placeholder here...">
                        <md-option value="optionone">Option 1</md-option>
                        <md-option value="optiontwo">Option 2</md-option>
                    </md-select>
                </md-input-container>

                <md-button type="submit" ng-click="eventFinish();">Done</md-button>
            </div>
        </form>
    </div>

JS

.controller("EventCtrl", function ($scope, Events) {
    $scope.currentEvent = null;
    $scope.events = Events;
    $scope.setEvent = function (id) {
      $scope.currentEvent = $scope.eve.detail[id];
    };

    $scope.addEvent = function() {
      $scope.events.detail[$scope.eve.id] = $scope.eve;
      delete($scope.eve);
    };
})


.factory("Event", function () {
    var Event = {};
    Event.detail = {
        "Sample": {
            "what": "What Here",
            "when": "When Here",
            "output": "Output here"
        }
    };
    return Event;
})

0 个答案:

没有答案