Search code examples
htmlangularjsscopeangularjs-scope

Angular - Controller can't see my variables in html


I'm here because i am not being able to make my controller see my html variables our functions, maybe you guys see something i dont =(

HTML:

<article ng-controller="CreateUserController">
  <div class="well">
    <center>
      <h1>Registrar</h1> <button ng-click="test()">Teste</button>
      </br>

      <form name="form">
        <div class="form-group has-feedback"
          ng-class="{
              'has-error': form.name.$invalid && form.name.$dirty,
            'has-success': form.name.$valid   && form.name.$touched
          }">
          <div>
            <input ng-disabled="true" type="name" required name="name" class="form-control" placeholder="Nome" ng-model="user.name">
            <p class="help-block" ng-messages="form.name.$error">
              <span ng-message="required">Nome esta vazio.</span>
            </p>
          </div>
        </div>
      </form>

    </center>
  </div>
</article>

JS:

 angular.module('auth').controller('CreateUserController', [
  '$scope', '$http', '$location','$state','$routeParams', '$modal','$rootScope',"$resource","Auth",'UserCompaniesService',
  function($scope, $http, $location,$state,$routeParams, $modal,$rootScope,$resource,Auth,UserCompaniesService){

    $scope.signedIn = Auth.isAuthenticated;
    $scope.logout = Auth.logout;

    var Account = $resource('/accounts/'+$routeParams.id+'.json',
                             {},
                             { "show": { "method": "GET" }});
    account = Account.get({"account_id":$routeParams.id});

    //Not being seen
    $scope.test = function() {
      alert('teste');
    }
    $scope.user.name = account.name;//$scope.user is not being seen
    $scope.user.email = account.email;

user.html.erb

<article ng-app="auth">
  <div ng-view></div>
</article>

Can you guys please help me?


Solution

  • It looks like you want to have an object called user on the $scope:

    $scope.user.  
    

    You have values tied to this object in the model. You have to create this object yourself in your controller's constructor:

    $scope.user = {};

    Then your model bindings will populate things like $scope.user.name on it from the input in the HTML.