Why bellow code does not work:
<!DOCTYPE html>
<html ng-app="demo">
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.6/angular.js"></script>
<script>
angular.module('demo',[])
.factory('myFactory', function(){
return {foo: function(){return 'bar';}};
})
.run(['myFactory', testFn(myFactory)]);
function testFn(myFactory){alert(myFactory.foo());}
</script>
</head>
<body ng-app="demo">
</body>
</html>
But bellow code works:
<!DOCTYPE html>
<html ng-app="demo">
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.6/angular.js"></script>
<script>
angular.module('demo',[])
.factory('myFactory', function(){
return {foo: function(){return 'bar';}};
})
.run(['myFactory', function(myFactory){
alert(myFactory.foo());
}]);
</script>
</head>
<body ng-app="demo">
</body>
</html>
In my application the function to run is lengthy so I don't want to put it inline the app.run because it looks really messy. How to do this?