2015-08-27 46 views
0

我有可能看起来像一个noob问题。我有一个简单的Angular控制器来控制页面上的表单。表单的标记非常简单。当我加载页面时,我看到表单呈现两次,而不是一次。我在这里做错了什么?HTML角度形式出现两次

这里是我的HTML:

<div class="container"> 
    <div class="col-lg-12"> 
     <div class="row"> 
      <h2>CREATE</h2> 
      <div class="col-lg-6 col-lg-offset-2"> 
       <div class="form-container" ng-controller="NewPostQuestionCtrl"> 
        <form accept-charset="UTF-8" name="form.postQuestionForm" ng-submit="createPostQuestion(postQuestion)" class="new_post_item" novalidate> 
         <input name="utf8" type="hidden" value="✓"> 
         <input name="authenticity_token" type="hidden" ng-model="postQuestion.token" ng-init="postQuestion.token='<%= form_authenticity_token %>'"> 
         <input name="employer_id" type="hidden" ng-model="postQuestion.employer_id" ng-init="postQuestion.employer_id='<%= current_employer.id %>'"> 

         <div class="form-group"> 
          <label>Question</label> 
          <textarea class="question-textbox" name="question" ng-model="postQuestion.question" required></textarea> 
         </div> 

         <div class="form-group"> 
          <label>Image</label> 
          <input class="" name="image" ng-model="postQuestion.image" type="file" required> 
         </div> 

         <div class="form-group"> 
          <label>Publish Question</label> 
          <select class="" name="published" ng-model="postQuestion.published" required> 
           <option value="true">Publish</option> 
           <option value="false">Don't Publish</option> 
          </select> 
         </div> 

         <input class="submit-btn" name="commit" type="submit" value="Publish Post" ng-disabled="form.postQuestion.$invalid"> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

这里是我的控制器:

app.controller('NewPostQuestionCtrl', ['$scope', '$http', function($scope, $http) { 
    $scope.form = {} 
    $scope.postQuestion = { 
     token: $scope.token, 
     employer_id: $scope.employer_id, 
     question: $scope.question, 
     published: $scope.published, 
     image: $scope.image 
    }; 
}]); 

谢谢!

回答

0

我不认为这会对任何人有所帮助,但我想出了我的问题,这是一个非常明显的疏漏。

我在嵌套在if/else语句中的application.html.erb中有两个<%= yield %>标记。第二个标签在声明之外,评估结果为true,导致我的声部呈现两次。

0

您是否有完整的HTML格式供您的应用程序使用?它只是呈现两次的形式还是还有h2元素?您的标记中是否可能有两次ng-view指令?

希望有帮助!

0

我认为它是加载两次。

检入您的.js文件,在其中将控制器映射到URL。如果在app.js或any_file.js中设置了控制器(在控制器和视图中映射url),则不必在html中包含ng-controller ..