使用ng-repeat生成的字段值在提交时未获得

时间:2014-11-08 07:02:07

标签: angularjs angularjs-controller angularjs-ng-form

表单提交的模板。此页面将显示表单模板。最初它显示了TItle,全名。点击“添加标签”'已生成链接新输入字段以输入标记。

在提交时,字段输入(story.tag)未包含在RequestPayload

<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: ""
      });
    };
  }

1 个答案:

答案 0 :(得分: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>

NoteController:

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);
    };
}