Angularjs 更新服务变量时不会触发监视

Angularjs 更新服务变量时不会触发监视,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我试图更改服务中的变量,但发生这种情况时,控制器中的监视不会触发: app.factory 'User', -> class User status: 'offline' set_status: (status) -> @status = status app.service 'Account', -> dsa: null test: 'asd' user: null app.controller 'MainCtrl', ($s

我试图更改服务中的变量,但发生这种情况时,控制器中的监视不会触发:

app.factory 'User',  ->
  class User
    status: 'offline'
    set_status: (status) ->
      @status = status

app.service 'Account', ->
  dsa: null
  test: 'asd'
  user: null

app.controller 'MainCtrl', ($scope, $timeout, Account, User) ->
  Account.user = new User
  $scope.$watch Account.user.status, (status) ->
    console.log "status changed to #{status}"

  $timeout ->
    Account.user.set_status 'busy'
    console.log Account.user.status
  , 2000

这里有一个例子。原因是什么?

美元手表没有问题。$watch表达式根据调用它的作用域求值,并将字符串表达式或函数作为其第一个参数。要使其工作,请在控制器内发布
帐户
,并通过字符串对其进行$watch:


或者将watch表达式更改为返回Account.user.status属性的函数。我不熟悉coffee脚本,所以我将把示例留给您。

看看这个答案,在这种情况下找到正确的方法:@maurizio使用$watch没有什么问题,除非确实有必要,否则不需要使用observer模式。@maurizio,我也不喜欢使用observer,但在我的例子中,$watchThanx man肯定有问题。我认为我可以使用它,而不必向作用域公开服务变量,这是我的错误。。。。
app.controller 'MainCtrl', ($scope, $timeout, Account, User) ->
  $scope.name = 'World'
  console.log Account
  Account.user = new User
  $scope.Account = Account
  $scope.$watch 'Account.user.status', (status) ->
    console.log "status changed to #{status}"

  $timeout ->
    Account.user.set_status 'busy'
    console.log Account.user.status
  , 2000