2016-02-24 27 views
0
<div ng-app="myApp"> 
    <div ng-controller="firstController"> 
    <form name=form1> 
    //there are some required fields here 
    <input type="button" ng-click="submit1(form1)"/> 
    </form> 
    </div> 

    <div ng-controller="secondController"> 
    <form name=form2> 
     // there are some required fields here 
     <input type="button" ng-click="submit2(form2)"/> 
     //I can check the status in controller by "$scope.form2.$valid" here 
    </form> 
    </div> 
    <div ng-controller="checkStatus"> 
    <input type="button" ng-click="submit3(form1,form2)"/> 
    //how can i check here 
    </div> 
</div> 

正如上面的代码,我有不同的控制器和不同的形式与它相关联。有一个部分,我需要检查所有表格状态 因为我是新的角js可以帮助我的任何人,如何解决这个问题。任何帮助,可能会感激。我有两种形式和两个控制器,现在我想检查角度js中不同控制器中这两种形式的状态?

回答

0

您可以有父窗体。如果任何它的孩子表格将视作无效,那么家长也将被无效:

<div ng-form="parentForm"> 
    ... two forms inside 
</div> 

然后检查parentForm.$valid

0

试试这个jsfiddle

只需将form更改为ng-form即可。并添加父母ng-form作为@karaxuna说。

function MyCtrl1($scope) { 
 
    $scope.test1 = "12"; 
 
}; 
 

 
function MyCtrl2($scope) { 
 
    $scope.test2 = "34"; 
 
}; 
 

 
function MyCtrl3($scope) {};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app> 
 
    <div ng-form="parentForm"> 
 
    <div ng-controller="MyCtrl1"> 
 
     <div ng-form="form1"> 
 
     <input required ng-model="test1"> 
 
     </div> 
 
    </div> 
 
    <div ng-controller="MyCtrl2"> 
 
     <div ng-form="form2"> 
 
     <input required ng-model="test2"> 
 
     </div> 
 
    </div> 
 
    <div ng-controller="MyCtrl3"> 
 
     <div ng-form="form3"> 
 
     <input required ng-model="test3"> 
 
     <pre>parentForm.$invalid = {{parentForm.$invalid}} 
 
     parentForm.$valid = {{parentForm.$valid}}</pre> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

0

使用jQuery

angular.element('#firstController').scope().submit2() 你可以在任何地方调用

采用了棱角分明

<div id="main" ng-controller="main"> 
<div id="firstController" ng-controller="firstController"> 
<form ></form> 
</div> 

<div id="secondController" ng-controller="secondController"> 
<form ></form> 
</div>  
</div> 

从第一控制器

$scope.$parent.submit2() 调用第二控制器功能你用你上面以前更应该将其设置为父范围。

相关问题