You expected function hoisting to happen:
myFunct();
function myFunct() {
alert('hey');
}
this would work.
But this wouldn't:
myFunct();
var myFunct = function() {
alert('hey');
}
The similar case is going on with the controller scope property, which behaves exactly as a regular variable in this case, means no hoisting happens.
You'll find some great explanations about hoising here: var functionName = function() {} vs function functionName() {}.
So, to make everything in your original code work using the hoisting feature, it should look like this:
pokeApp.controller('mycontroller', function($scope, $routeParams){
// part 1:
myFunc();
function myFunc(){
console.log("Hi !");
}
// part 2:
helloWorld();
function helloWorld(){
console.log("Hello World");
}
}
Or, a little hacky way to maintain scopes:
pokeApp.controller('mycontroller', function($scope, $routeParams){
// part 1:
$scope.myFunc = myFunc; // this is the key, assigns a hoisted function value
// to the $scope object property which is then ready
$scope.myFunc();
function myFunc(){
console.log("Hi !");
}
// part 2:
this.helloWorld = helloWorld;
this.helloWorld();
function helloWorld(){
console.log("Hello World");
}
}
Here's a snippet showing that in action:
var myObj = {};
myObj.f = myFunct;
myObj.f();
function myFunct() {
alert('yay, it still works!');
}