LoginSignup
5
5

More than 5 years have passed since last update.

FormControllerをControllerからごにょごにょする

Posted at

HTML:

foo.html
<div ng-conroller="FooCtrl">
  <form name="form1">
    <div ng-init="putForm(form1)"></div>
    <label><input>・・・</input></label>
  ...
  </form>
  <form name="form2">
    <div ng-init="putForm(form2)"></div>
    <label><input>・・・</input></label>
  ...
  </form>
  <button ng-click="register()">Register</button>
</div>

Controller:

foo.js
function FooCtrl($scope){
  $scope.putForm = function(form){
     if (!$scope._forms) {
            $scope._forms = {};
     }
     $scope._forms[form.$name] = form;
  };

  $scope.register = function(){
    if($scope._forms.form1.$invalid || $scope._forms.form2.$invalid){
      window.alert("invalid!!");
      return;
    }
    ...
  };
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5