Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 当单个服务加载数据时,通知多个控制器_Angularjs - Fatal编程技术网

Angularjs 当单个服务加载数据时,通知多个控制器

Angularjs 当单个服务加载数据时,通知多个控制器,angularjs,Angularjs,情景: 我有两个控制器,它们使用一个服务,第一个控制器可以调用搜索方法,并在服务加载数据时获得结果,第二个控制器只能被通知服务加载数据 这是我的代码: app.factory "searchService", ($resource, $q, $log) -> resource = $resource("//localhost:8001/data/search/1") searchSuccessDef = $q.defer() searchSuccess: searchSuc

情景: 我有两个控制器,它们使用一个服务,第一个控制器可以调用搜索方法,并在服务加载数据时获得结果,第二个控制器只能被通知服务加载数据

这是我的代码:

app.factory "searchService", ($resource, $q, $log) ->
  resource = $resource("//localhost:8001/data/search/1")

  searchSuccessDef = $q.defer()

  searchSuccess: searchSuccessDef.promise

  search: (scope, term) ->
    def = $q.defer()

    resource.get(

    ((content) ->
      $log.info content
      searchSuccessDef.notify()
      def.resolve(content)
    ),

    ((response) ->
      $log.warn response
      def.reject(response)
    )
  )

  def.promise

  ///

app.controller "navpanelController", ($scope, searchService) ->

  $scope.search = (term) ->

   searchResult = searchService.search(term)

   searchResult.then (res) ->

    console.log "!!!"

    $scope.result = res

 ///

app.controller "contentController", ($scope, $q, searchService) ->

 searchService.searchSuccess.then (content) ->
  console.log "---"
但是这不起作用,因为
searchSuccessDef.notify()
不调用notifyer
searchService.searchSuccess.then(content)->
,但是如果我改用
searchSuccessDef.resolve()
,这会触发一次承诺

这就是问题所在。
谢谢

我不熟悉CoffeesSript,但我可以告诉您,notify方法回调是
then
方法的第三个参数

.then(function(greeting) {   //Success
   alert('Success: ' + greeting);
 }, function(reason) {      //Failure
   alert('Failed: ' + reason);
 }, function(update) {      //notification
   alert('Got notification: ' + update);
});

谢谢,感觉自己好了,花了两个小时,顺便说一句,这咖啡是我的错,我把它修好了。