Click here to Skip to main content
13,151,125 members (41,816 online)
Rate this:
 
Please Sign up or sign in to vote.
I found some weird things in below code: the mytest function is called 3 times!!! WHY ?

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>


Test : {{mytest()}}


<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name= "John ";

$scope.mytest = function () {
console.log('my test');
return 'something';
};

});
</script>

</body>
</html>

		
Posted 3-Oct-15 3:46am
tuan21361
Comments
John C Rayan 6-Oct-15 8:58am
   
I don't think your code will work. You have to add

ng-app="myApp" ng-controller="myCtrl" in body. See below for the amended code.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<!DOCTYPE html>
 
<html>
 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
 
<body ng-app="myApp" ng-controller="myCtrl">
 

 

 

 
Test : {{mytest()}}
 

 

 
<script>
 
    var app = angular.module('myApp', []);
 
    app.controller('myCtrl', function ($scope) {
 
        $scope.name = "John ";
 

 
        $scope.mytest = function () {
 
            console.log('my test');
 
            return 'something';
 
        };
 

 
    });
 
</script>
 

 
</body>
 
</html>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 6 Oct 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100