1

表单提交模板。此页面将显示表单模板。最初它显示标题,全名。单击“添加标签”链接会生成用于输入标签的新输入字段。

提交时,RequestPayload 中不包含字段 input(story.tag)

<form novalidate ng-submit="save()">

    <div>
        <label for="title">Title</label>
        <input type="text" ng-model="story.title" id="title" required/>
    </div>

    <div>
        <label for="firstName">Full Name</label>
        <input type="text" ng-model="story.fullname" id="fullname" required/>
    </div>

    <div ng-controller="Note" >
      <div ng-repeat="story in items ">
        <label>Tag {{$index+1}}:</label>
        <input type="text" ng-model="story.tag" id="tag" required/>
      </div>
      <a ng-click="add()">Add Tags</a>
    </div>


    <button id="save" class="btn btn-primary">Submit Story</button>
</form>

脚本:- app.js

angular.module("getbookmarks", ["ngResource"])
.factory('Story', function ($resource) {
    var Story = $resource('/api/v1/stories/:storyId', {storyId: '@id'});
    Story.prototype.isNew = function(){
        return (typeof(this.id) === 'undefined');
    }
    return Story;
})
.controller("StoryCreateController", StoryCreateController);


function StoryCreateController($scope, Story) {

   $scope.story = new Story();

   $scope.save = function () {
      $scope.story.$save(function (story, headers) {
         toastr.success("Submitted New Story");
      });
   };
}

//add dynamic forms
 var Note = function($scope){
    $scope.items = [];

    $scope.add = function () {
      $scope.items.push({ 
        inlineChecked: false,
        tag: "",
        questionPlaceholder: "foo",
        text: ""
      });
    };
  }
4

1 回答 1

0

ng-repeat 中的故事对象在另一个范围内。这个JSFiddle应该可以满足您的需求。

<div ng-app>
    <div ng-controller="NoteCtrl">
        <form novalidate ng-submit="save()">
            <div>
                <label for="title">Title</label>
                <input type="text" ng-model="story.title" id="title" required/>
            </div>
            <div>
                <label for="firstName">Full Name</label>
                <input type="text" ng-model="story.fullname" id="fullname" required/>
            </div>
            <div ng-repeat="story in items">
                <label>Tag {{$index+1}}:</label>
                <input type="text" ng-model="story.tag" required/>
            </div> <a ng-click="add()">Add Tags</a>

            <button id="save" class="btn btn-primary">Submit Story</button>
        </form>
        <div ng-repeat="test in items">
            <label>Tag {{$index+1}}: {{test.tag}}</label>
        </div>
    </div>
</div>

笔记控制器:

function NoteCtrl($scope) {
    $scope.story = {};
    $scope.items = [];
    $scope.story.tag = $scope.items;

    $scope.add = function () {
        $scope.items.push({
            inlineChecked: false,
            tag: "",
            questionPlaceholder: "foo",
            text: ""
        });
        console.log($scope.story);
    };
}
于 2014-11-08T10:50:50.367 回答