0

I'm learning AngularJS by doing my first app with it. As far as now, everything was working ok and I'm pretty excited :)

But now I have a console.log that is totally confusing me, and I'm start thinking I've completely missed something.

I have a simple tag bound to a controller:

<h2 ng-controller='HeaderController' ng-show='data.actualSection > 0'>
    {{titleForSection()}}
</h2>

The controller is very simple:

    uxctModule.controller ('HeaderController', function ($scope, ModelData){
    $scope.data = ModelData;

    $scope.titleForSection = function () {
        console.log ("RETURNING IT");
        return ("I SHOULD RETURN YOU SOMETHING");
    }
});

What's really confusing me is that I've noticed that every time something is changing in the model, the function is triggered. How can the controller be executing the function constantly without a $watch in it?

5
  • 1
    what is the purpose of titleForSection() in your template? In general should bind function calls to buttons, etc. but not put them directly in templates.
    – Sebastian
    Commented May 5, 2014 at 16:42
  • @Sebastian the reason is that the label is going to be dynamic, and the controller should provide the right label for it
    – Sr.Richie
    Commented May 5, 2014 at 16:49
  • 1
    I think what he is saying is don't bind the function to the template. Call the function in the controller and assign the output to a variable in the scope which would be used in the template. Angular works by dirty-checking the scopes, so it will call your function over and over again...
    – aet
    Commented May 5, 2014 at 16:57
  • @Sebastian you should post it as the answer because my wrong approach was the reason of the function repeating over and over again. Thank you
    – Sr.Richie
    Commented May 5, 2014 at 17:22
  • @aet you should post it as the answer because my wrong approach was the reason of the function repeating over and over again. Thank you
    – Sr.Richie
    Commented May 5, 2014 at 17:22

2 Answers 2

4

Data binding in angular is done via a digest loop, which means that angular loops over and over again checking for changes, and in the case of a function that is bound to the function must be evaluated looking for changes.

This is the reason it is generally a bad idea to bind your UI to the result of a function. Instead you should do something like this:

Markup:

<h2 ng-controller='HeaderController' ng-show='data.actualSection > 0'>
    {{sectionTitle}}
</h2>

Controller:

$scope.sectionTitle = 'I SHOULD RETURN YOU SOMETHING';
//and update it dynamically
$scope.funcToUpdateTitle = function(newTitle){
    $scope.sectionTitle = newTitle;
};
1

Actually in angularJS all functions related to view will be called as and when a digest cycle is called in that way since you have called titleForSection() in your HTML so when where an event occurs in the HTML, causes the function to be executed.

Hope it helps!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.