【发布时间】:2014-02-05 15:05:57
【问题描述】:
我是 AngularJS 的初学者,我想为每个视图创建一个控制器,但是这个文件只有在我加载视图时才能被引用,所以:
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';
}
}
在视图中我设置了控制器的名称并放置了一个指令来测试:
<h1>Página login!</h1>
<div ng-controller="loginCtrl">
<p ng-bind="teste"></p>
</div>
在我设置的控制器中:
app.controller('loginCtrl', function($scope)
{
$scope.teste = 'loginCtrl';
});
我这样做了,但 $scope.teste 的值没有改变。我该如何解决这个问题?
【问题讨论】:
标签: javascript angularjs angularjs-routing angularjs-controller