I'm beginner in AngularJS and I want to create a controller to each view, but this file only can be referenced when I load the view, so:
my app.js:
var app = angular.module('app',['ngRoute']);
app.config(function($routeProvider)
{
$routeProvider
.when('/', {
templateUrl : 'app/views/login.html'
})
.when('/calendar', {
templateUrl : 'app/views/calendar.html'
})
.otherwise ({ redirectTo: '/' });
});
app.run( function($route, $rootScope, $location)
{
$rootScope.$on( "$routeChangeStart", function()
{
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = load_script($location.path());
document.body.appendChild(s);
});
});
function load_script(location)
{
switch(location)
{
case '/' : return 'app/controllers/loginController.js';
case '/calendar' : return 'app/controllers/calendarController.js';
}
}
And in the view I set the name of the controller and put a directive to test:
<h1>Página login!</h1>
<div ng-controller="loginCtrl">
<p ng-bind="teste"></p>
</div>
In controller I set:
app.controller('loginCtrl', function($scope)
{
$scope.teste = 'loginCtrl';
});
I did this but the value of $scope.teste don't change. How can I solve this problem?