Angular - Controller无法在html中查看我的变量

时间:2015-12-18 19:48:34

标签: html angularjs scope angularjs-scope

我在这里,因为我无法让我的控制器看到我的html变量我们的功能,也许你们看到的东西我不=(

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>

你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

看起来你想在$ scope上有一个名为user的对象:

$scope.user.  

您在模型中将值绑定到此对象。您必须自己在控制器的构造函数中创建此对象:

$ scope.user = {};

然后您的模型绑定将填充类似的东西 来自HTML输入的$ scope.user.name。