1

Using AngularJS 1.1.5 with CoffeeScript I'm trying the use the new 'Controller as ...' syntax as follows:

class EventCtrl
    @$inject = ['$scope', '$http']

    constructor: (@$scope, @$http) ->
        @$http.get('/some/url/', config)
            .success (data) ->
                #set the events directive
                @events = data
            .error (data) ->
                console.log('error!')

app.controller('EventCtrl', EventCtrl)

With the following HTML fragment:

<div ng-controller="EventCtrl as ctrl">
    <div>{{ctrl.events}}</div>
</div>

This works alright except for the fact that changes in @events don't update (the binding point in) the template. The 'data' incoming in the success handler of the @$http.get is looking ok.

This code has been working with a previous version of AngularJS with the regular none-class controllers.

UPDATE One (ugly) solution turns out to be explicity setting this (@) to a local value in the method (thiz in the example below). Not very elegant but it works.

class EventCtrl
    @$inject = ['$scope', '$http']

    constructor: (@$scope, @$http) ->
        thiz = @
        @$http.get('/some/url/', config)
            .success (data) ->
                #set the events directive
                thiz.events = data
            .error (data) ->
                console.log('error!')

SOLUTION Use a fat arrow for the success handler. The constructor is automatically attached to the instance so a fat arrow is not needed there (it would have been if it weren't a constructor).

class EventCtrl
    @$inject = ['$scope', '$http']

    constructor: (@$scope, @$http) ->
        @$http.get('/some/url/', config)
            .success (data) =>
                #set the events directive
                @events = data
            .error (data) ->
                console.log('error!')

1 Answer 1

2

You're supposed to attach it to scope, not @ (which might not even be your class instance, if .success changes the context. You can use coffeescript's fat arrow for that).

Also, what's the point of having an init method ? It's exactly the constructor's purpose.

Sign up to request clarification or add additional context in comments.

4 Comments

Normally you would make it an instance attribute but it indeed seems that @ inside the http.get is not the class but the window. A solution is to explicitely set @ to the instance at the beginning of the method. Oh, and yes, you are indeed right that there is no need for the init(), it was a left-over because I stripped a lot of other code.
As I said, you can just use CoffeeScript's => (fat arrow) to keep your this
You cannot use a fat arrow on a constructor (cannot define a constructor as a bound function), but using it on the success handler indeed solves the issue. Thanks!
I meant to use it on the success handler, indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.