我正在尝试创建一个控制器,它等待在指令中解析延迟的承诺。但我得到的只是一个无声的失败,阻止我的控制器和指令执行或做任何事情。
app.coffee
angular.module('myApp', [
'ngRoute'
'myApp.filters'
'myApp.services'
'myApp.directives'
'myApp.controllers'
]).config [ '$routeProvider', ($routeProvider) ->
$routeProvider.when '/',
template: '<div></div>'
controller: 'MyCtrl1'
resolve:
CM: ['CM', (CM) ->
CM
]
return
])
controllers.coffee
angular.module('myApp.controllers', [])
.controller('MyCtrl1', ['$scope', 'CM', ($scope, CM) ->
# do stuff with a resolved CM
])
directives.coffee
angular.module('myApp.directives', [])
.directive('myDirv', ['CMd', (CMd) ->
(scope, elm, attrs) ->
CMd.CMObject.applyToDiv elm
CMd.deferred.resolve CMd.CMObject
scope.apply() # I've tried it with and without scope.apply()
return
])
services.coffee
angular.module('myApp.services', [])
.service('CMd', ['$q', ($q) ->
@CMObject = CMObject
@deferred = $q.defer()
return
])
.factory('CM', ['CMd', (CMd) ->
CMd.deferred.promise
])
为什么这会导致我的控制器和我的指令无声地失败?如何让我的控制器正确等待解决?