2015-10-07 54 views
3

我有以下形式(導軌)

= form_for([@debate,@argument], html: { class: "form-horizontal", role: "form" , "ng-submit"=>"addArgument('support',$event)", novalidate: true, name: "argumentformsupport"}) do |f| 
    - if @argument.errors.any? 
    .alert.alert-danger.alert-dismissable role="alert" 
     button.close type="button" data-dismiss="alert" 
     span aria-hidden="true" 
      | × 
     span.sr-only 
      | Close 
     h4= "#{pluralize(@argument.errors.count,"error")} prohibited this argument from being saved:" 
     ul 
     - @argument.errors.full_messages.each do |msg| 
      li= msg 
    = f.hidden_field :side, :value=>side, "ng-model"=>"argument['support'].side" 

    .form-group 

    .col-sm-12 
     = f.text_area :content, class: "form-control", "ng-model"=>"argument['support'].content", required: true, "ng-minlength"=>"100", "ng-maxlength"=>"4000" 
     textarea.form-control ng-model="argument['support'].content" name="content" required=true ng-minlength="100" ng-maxlength=4000 
     <div ng-messages="argumentformsupport.content.$error" style="color:maroon" role="alert"> 
     <div ng-message="required">Please type the argument</div> 
     <div ng-message="minlength">Your field is too short</div> 
     <div ng-message="maxlength">Your field is too long</div> 
     </div> 

    .form-group 
    .col-sm-4 
     = f.submit class: "btn btn-success" 
    .col-sm-3 
     button.btn.btn-danger type="button" ng-click="debcon.toggleArgumentForm('support')" Cancel 

以下是我的應用程序的js代碼

var debateApp=angular.module("debate",['ng-rails-csrf', 'yaru22.angular-timeago']) 

debateApp.controller("DebateController",["$scope","$http",function($scope,$http){ 
.... 
} 

黃氏的消息的事情並不在所有的工作。所有消息都可見。我添加了角度消息依賴性,但所有消息仍然可見。

我還檢查argumentformsupport.content。$錯誤對象。其空

+0

您正在使用的角版本? –

+0

我正在使用angular 1.4.5 –

+2

你在哪裏添加了依賴關係?在你的例子中缺少它。你可以設置一個重啓器來演示這個問題,這樣我們可以調試它嗎? –

回答

4

你的主要模塊丟失ngMessages依賴。應該看起來像這樣:

angular.module("debate",['ngMessages', 'ng-rails-csrf', 'yaru22.angular-timeago']) 

當然,你還需要包括正確的JavaScript文件。

參見:https://docs.angularjs.org/api/ngMessages;

0

首先,你在你的模塊依賴陣列添加ngMessages?

angular.module("debate",['ngMessages', 'ng-rails-csrf', 'yaru22.angular-timeago']) 

其次,我建議你在ng-messages div中使用$ dirty(檢查用戶是否已經與表單交互)。它看起來像:

 <div ng-messages="argumentformsupport.content.$error" ng-show="argumentformsupport.content.$dirty" style="color:maroon" role="alert"> 
+0

完成這兩個。沒有幫助 –