문제

서비스에서 변수를 변경하려고하지만 컨트롤러에서 시계가 발생하지 않을 때 트리거되지 않습니다 :

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 표현식은 on on이라고 불리는 범위에 대해 평가되며 문자열 표현식이나 함수를 첫 번째 인수로 사용합니다.컨트롤러 내부에 Account를 Scope로 게시하고 문자열을 통해 $ WAST를 사용하도록하십시오.

데모 Plunker

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
.

또는 occount.user.status 속성을 반환하는 함수로 시계 표현식을 변경하십시오.나는 커피 스크립트에 익숙하지 않으므로 예를 당신에게 맡길 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top