3

I'm a total newbie to javascript. Was just wondering what is the difference between the following when using the controllerAs approach, and why the second approach doesn't work:

angular.module("app")
        .controller("angularController", angularController);

function angularController() {
    this.hello = "hello";
    this.goodbye = "goodbye";
}

and

angular.module("app")
        .controller("angularController", angularController);

function angularController() {
    var hello = "hello";
    var goodbye = "goodbye";

    return {
        hello: hello,
        goodbye: goodbye
    }
}
Met-u
  • 555
  • 7
  • 18
  • Because there is nowhere for a controller to return to. That's not how they work. There are numerous patterns for javascript functions and they get used accordingly. angular does not use that pattern for controllers – charlietfl Sep 29 '16 at 13:34
  • It **does** work. Maybe you use an older version of Angular. – a better oliver Sep 29 '16 at 13:45
  • @chartlietfl hmmm.... makes sense. Basically you saying in javascript what I've done will work either way depending on how you use the function and Angular doesn't use the function in such a way that the second approach will when using Angular-controllers? – Met-u Sep 29 '16 at 13:49
  • @zeroflagL I'm using AngularJs 1.3. Nice to know they have added support for it in later versions if that's the case :) – Met-u Sep 29 '16 at 13:53
  • I'm not sure that this should have been marked as a duplicate, but the code that the OP provided **definitely works** in angular 1.5.8. http://plnkr.co/edit/8QPOUUSM3xm4bmLr2nr9?p=preview – Claies Sep 29 '16 at 14:01
  • They had to add support to enable the use of ES6 classes. – a better oliver Sep 29 '16 at 14:03
  • @charlietfl Thanks for the link to that SO question. The answer makes a lot of sense. Should I delete this question or leave it here to help with google searches? – Met-u Sep 29 '16 at 14:08
  • @charlietfl It's not a duplicate of the provided question. It's an Angular related problem and the second approach works in newer versions. – a better oliver Sep 29 '16 at 14:12
  • @zeroflagL i didn't actually mark as duplicate, bergi did , however consider that Op is beginner and that behavior is not documented and OP is better off learning to use documented approaches – charlietfl Sep 29 '16 at 14:16
  • @charlietfl I see. I'm not sure what you mean by _"not documented"_. In JavaScript a constructor is allowed to return an object. The fact that the second approach was not working in previous versions of Angular actually was the thing that was not documented. – a better oliver Sep 29 '16 at 14:22
  • @zeroflagL but it worked and does continue to work as documented. How does that argument help someone who is a complete beginner – charlietfl Sep 29 '16 at 14:23
  • I think the question is quite valid. The OP is asking why the method for creating a factory in angular didn't work for creating a controller. This is something that is supported in the newest releases of angular, but regardless, it's not a duplicate of the generic JavaScript concept. – Claies Sep 29 '16 at 14:51

3 Answers3

0

The function you register with angular.module.controller is not a factory, it's a constructor.

The second approach is more often used with Angular services.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
  • _"The function you register with angular.module.controller is not a factory, it's a constructor"_ So? – a better oliver Sep 29 '16 at 13:46
  • Yes, the second approach is used with Angular factories. I was just not sure why I couldn't use the same approach for controllers. And being that the controller is a constructor, does that mean it will return void and not an object? – Met-u Sep 29 '16 at 13:50
  • Constructors are constructed with `new` and thus the reference to the constructed object is implicitly returned. A factory function is simply invoked and is expected to return the constructed object. But I guess this is already covered in the other question :P – Oliver Salzburg Sep 29 '16 at 16:35
-3

Change this in your second approach

this.hello:hello;
this.goodbye:goodbye;

instead of

hello:hello;
goodbye:goodbye;
Pradeepb
  • 2,564
  • 3
  • 25
  • 46
khush
  • 2,702
  • 2
  • 16
  • 35
-3

In the second approach you return tha values in the function call, this is why dont work, the HTML dont call functions, the HTML access propierties.

In the first approach the values are propierties of the controller, this make possible you access the values from the HTML.

Scheffer
  • 96
  • 1
  • 7
  • HTML supply attributes. JavaScript objects supply properties. Prof: http://lucybain.com/blog/2014/attribute-vs-property/ – Eugen Konkov Sep 29 '16 at 14:13