0

This is my controller.

angular.module('homePage').controller('buttonViewCtrl',  [function() {
    console.log(this);
    this.buttonDisplay = false;
    console.log(this);

    this.nav = function() {
        console.log(this);  //returns Object {buttonDisplay: false}
        //console.log(JSON.stringify(this));
        this.buttonDisplay = true;
    };

    this.closeNav = function() {
        console.log(this); //returns Object {buttonDisplay: true}
        //console.log(JSON.stringify(this)); 
        this.buttonDisplay = false;
    };
}])

The first console.log method logs an empty object.

The second console.log method logs the controller object with a property buttonDisplay added to it. enter image description here

The third console.log method (in nav()) logs the controller object with all its methods like so: enter image description here

The fourth console.log method logs the same object with the buttonDisplay property changed.

enter image description here

I have two questions.

1) From this example, as per my understanding, when Angular sees a controller definition it declares an empty object. It then attaches properties to the object as they are executed. My question is that how come when the nav() method is triggered, it detects that there are other methods in the controller and attaches them to the controller object too (closeNav() in this case). The behavior I expected was it to attach only the nav() method when it was triggered, and when closeNav() would be triggered it would attach that too to the controller object. As you can see though, in figure 3, Angular has attached closeNav() to the controller without it being triggered. How is Angular doing this?

2) According to the best practices of Angular, we should not manipulate the DOM in the controller. In my controller, the nav() function dispalys a navigation bar template and the closeNav() function closes it. Is this considered as DOM manipulation? Am I adding too much presentation logic in my controller?

5
  • 2
    1. It top to bottom execution which shows that while you are doing first console.log at that nav & closeNav methods were not registered thats why they are not getting printed in console., 2. Its not DOM manipulation as you are only playing with a flag and on basis of which you are showing and hiding element of html using directive which looks fine Commented Aug 7, 2015 at 17:40
  • Pankaj is correct... and where do you get the idea from that "only when triggered" a method is attached to the object? That would seem rather bizarre... Controller function is really a constructor function for the controller object Commented Aug 7, 2015 at 17:44
  • Regarding #2, this is not considered direct DOM manipulation as you are changing values which are on the scope. These values are updated via binding to the directives in your view (the HTML). These directives are the ones that are causing the change. This is why when you need to change the DOM directly, you should create directives. Commented Aug 7, 2015 at 17:44
  • @PankajParkar I am sorry, but I don't think you understood my first question. My question was that how Angular attaches closeNav at the third console.log statement itself? You can look at @plamut's answer. It is very informative. Commented Aug 7, 2015 at 20:40
  • @NewDev Thanks for commenting. I don't think you understood my question though. Please see plamut's answer it is very informative and answers my question perfectly. Commented Aug 7, 2015 at 20:43

1 Answer 1

1

1) From this example, as per my understanding, when Angular sees a controllerdefinition it declares an empty object.

Actually no, Angular just remembers that a controller has been registered under some name. The controller is instantiated when Angular determines that it is needed, e.g. when encountering a DOM node with the ng-controller="..." directive. At that point it calls new ControllerFunction(...), passing in all of its declared dependencies as arguments.

The first two console.log() calls thus execute while the controller is still being initialized, and that's the reason you don't see all of its members there yet.

2) According to the best practices of Angular, we should not manipulate the DOM in the controller. In my controller, the nav() function dispalys a navigation bar template and the closeNav() function closes it. Is this considered as DOM manipulation?

No, it isn't, that's perfectly fine. After all it's controller's job to provide all the data the view (template) needs. Direct DOM manipulation would be if you, for example, start creating new DOM elements, directly manipulating their attributes, registering event handlers on them, etc. (that kind of stuff belongs to directives).

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

Comments

Your Answer

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