title: Angular.js accessing model from controller (server validation)
body: <p>i now trying to migrate my legace app to Angular and now stuck with following issue.
  I have this simple form:</p>
  
  <pre><code>&lt;form name="loginForm" ng-controller="LoginCtrl" ng-submit="doLogin()"&gt;
    &lt;input type="text" name="login" ng-model="login.login" /&gt;
    &lt;span class="errors" ng-show="loginForm.login.$error.required"&gt;Required&lt;/span&gt;
    &lt;span class="errors" ng-show="loginForm.login.$error.bad_login"&gt;Login or pass incorrect&lt;/span&gt;
    &lt;input type="password" ng-model="login.pass"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Server returns error as this type of JSON: { login: 'bad_login', pass: 'incorrect' }
  And in controller:</p>
  
  <pre><code>function LoginCtrl($scope, $http) {
  
      $scope.doLogin = function() {
          var model = $scope.login;
          var req = { "login": model.login, "pass": model.pass };
          $http({ method: "POST", url: "/login", data: req }).success(function (resp) {
              if(resp.status != "ok") {
                  angular.forEach(resp, function (value, key) {
                      // ex: model['login'].$setValidity('bad_login', false);
                      model[key].$setValidity(value, false);
                  });
              }
          });
      };
  }
  </code></pre>
  
  <p>I try to mark each field in loop, not to hardcode every field by hand. but this approach not working. I get this error: <code>TypeError: Cannot call method '$setValidity' of undefined</code>, even when field is left empty i have another error: <code>TypeError: Cannot read property 'login' of undefined</code>
  So i think i need to get instance of model for every field in form, any ideas ?</p>