0

I'm trying to create a controller which waits for a promise who's deferred is resolved in a directive. But all I get is a silent failure, preventing both my controller and directive from executing or doing anything.

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 
])

Why does this cause both my controller and my directive to silently fail? How can I get my controller to wait for a resolve correctly?

4

0 回答 0