1

在我的主要布局中,我有一个标题,例如:

<h1>{{ heading }}</h1>

我可以使用以下方法将其设置为控制器中的各种静态值:

$rootScope.heading = 'My Heading';

但是,如果我尝试从资源创建动态标题,则其为空白:

app.controller('ShowAccountController', function($scope, $rootScope, $state, $stateParams, Account) {
  $scope.account = Account.get({ id: $stateParams.id });
  $rootScope.heading = $scope.account.name;
});

帐户变量肯定设置在那里(通过控制台检查)所以我有点难过为什么标题没有值?

4

1 回答 1

1

正如@sh0ber 指出的那样,您对 Chrome 控制台的工作方式感到困惑。无论如何,要设置heading变量,您应该使用回调:

app.controller('ShowAccountController', function($scope, $rootScope, $state, $stateParams, Account) {
  $scope.account = Account.get({ id: $stateParams.id }, function(account){
    $rootScope.heading = account.name;
  });
});
于 2013-06-19T04:50:55.947 回答