2013-12-21 40 views
4

我有3種不同的表單頁面,它們是在引導模式窗口中使用ng-include插入到DOM中插入的。在這種情況下,在每種表單中進行驗證並完成表單提交(對於所有3種表單),最好的方法是什麼?如何驗證窗體插入時使用ng-include

HTML

<div ng-switch on="page"> 
    <div ng-switch-when="Games"> 
     <div ng-include="'Games.html'"></div> 
    </div> 
    <div ng-switch-when="Music"> 
     <div ng-include="'Music.html'"></div> 
    </div> 
    <div ng-switch-when="Videos"> 
     <div ng-include="'Videos.html'"></div> 
    </div> 
</div> 

演示:http://plnkr.co/edit/D1tMRpxVzn51g18Adnp8?p=preview

+0

你想使用獨特的表單還是需要在每種表單中進行驗證?我的意思是你必須驗證比音樂更好的遊戲...... – Whisher

+0

我不認爲你的問題很清楚你想要達到的目標。請考慮修改。 – eddiec

+0

我已經修改了描述。當我點擊提交時,我需要驗證所有3種形式,並且如果可能,請更改左側導航文本顏色(遊戲,音樂..),以突出顯示錶單未填充/無效。 – user1184100

回答

0

還有就是要找到一種方法來驗證數據尚未 (可能是jqueryvalidation),但可以是一個起點。 我覺得沒有辦法讓遊戲的價值$有效 所以我認爲

var app = angular.module("myApp", []) 
      app.controller("FormsCtrl", function($scope) { 
       //console.log($scope); 
       // $scope.items = ['Games', 'Music', 'Videos']; 
       $scope.$on('someEvent',function(e,a){ 
        console.log(a); 
       }) 
      }); 
      app.directive("myform", function() { 
       return { 
        restrict: "A", 
        link:function(scope,element,attrs){ 
         element.bind('submit',function(e){ 
          var isValid = false; // TO DO :) 
          scope.$emit('someEvent', [attrs.fname,isValid]); 
         }); 
        } 
       } 
      }); 



<div ng-controller="FormsCtrl"> 
     <div ng-switch on="page"> 
      <div ng-switch-when="Games"> 
       <div ng-include="'Games.html'"></div> 
      </div> 
      <div ng-switch-when="Music"> 
       <div ng-include="'Music.html'"></div> 
      </div> 
      <div ng-switch-when="Videos"> 
       <div ng-include="'Videos.html'"></div> 
      </div> 
    </div> 
</div> 


<form name="games" class="simple-form" myform fname="games"> 
    <input type="text" ng-model="prefix.games.name" name="uName" required /><br> 
</form> 

編輯 更聰明更快捷的方法:)

app.controller("FormsCtrl", function($scope) { 
       $scope.mySubmit = function(isValid){ 
        console.log(isValid); 
       } 
      }); 


<form name="games" class="simple-form" ng-submit="mySubmit(games.$valid)"> 
    <input type="text" ng-model="prefix.games.name" name="uName" required /><br> 
</form>