0

I'm currently getting to grips with angular and I'm a complete new guy to it, I've put together a jsfiddle for the issue.

But in short I'm not sure why my function attached to the angular scope is called twice.

Script

var registrationModule = angular.module("registrationModule", []);
registrationModule.controller("NavigationController",  function ($scope) {
    $scope.menuItems = [{ text: 'Home'}];
    alert("hi");
    $scope.getClass = function (path) {
        alert("getClass");
        return "frank";
    };
});

HTML

<body ng-app="registrationModule">
<div class="collapse navbar-collapse navbar-responsive-collapse">
    <ul class="nav navbar-nav navbar-right" ng-controller="NavigationController">
        hi {{ getClass(); }} butcher
    </ul>
</div>
</body>

http://jsfiddle.net/6F55K/1/

Any tips?

1
  • it's the angularjs digest cycle Commented Mar 19, 2014 at 19:59

2 Answers 2

2

The Angular digest loop is built to work this way. Any expressions in your view will be executed at least once.

When the view is compiled it will call your getClass() function to get a value, then it will run the digest loop again to see if anything has changed since its previous call. This is how it knows that your view is up to date.

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

Comments

0

Noel's answer didn't work for me. How about changing the function so it sets a variable?

registrationModule.controller("NavigationController",  function ($scope) {
    $scope.menuItems = [{ text: 'Home'}];
    alert("hi");
    $scope.setClass = function (path) {
        alert("setClass");
        $scope.class = "frank";
    };
});

Check out this fiddle: http://jsfiddle.net/6F55K/3/

1 Comment

I thought it was an issue with route registration... it didn't work for me either :)

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.