【发布时间】:2016-03-24 14:50:27
【问题描述】:
我想管理特定于 ng-view 页面的控制器,因此我将控制器放在 ng-view 页面中并仅使用特定于该页面的控制器。但是,该代码并未显示该控制器应显示的内容。
这是我的情况。 我将代码分成 3 个文件,分别是“mainfile.php”、“page1file.php”和“page2file.php”。 “mainfile.php”包含路由到第 1 页和第 2 页的主页。为了比较结果,我为这 2 个页面创建了不同的条件。 “page1file.php”使用的是在“mainfile.php”中定义的控制器,而“page2file.php”使用的是在页面本身中定义的控制器。 p>
在这种情况下,“page1file.php”成功显示了我想要的内容,但“page2file.php”没有显示它应该显示的内容。请帮助我并给出一个非常简单的解释和一个简单的解决方案,因为我对 angularjs 很陌生。
这是我的代码。您可以复制它们并在您的 php 服务器上运行它。
mainfile.php:
<!DOCTYPE html>
<html>
<head>
<title>learn angular routing</title>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
<script type="text/javascript">
function routeReload($scope) {
$scope.routeReloading = function(){
window.location.reload();
};
}
routeReload.$inject = ['$scope'];
function routeConfig($routeProvider) {
$routeProvider
.when('/one',{
templateUrl:'page1file.php'
})
.when('/two',{
templateUrl:'page2file.php'
})
;
}
routeConfig.$inject=['$routeProvider'];
function testOne($scope) {
$scope.name = "page one";
}
testOne.$inject = ['$scope'];
var testNgView = angular.module('testNgView',['ngRoute']);
testNgView.config(routeConfig);
testNgView.controller('routeReload',routeReload);
testNgView.controller('testOne',testOne);
</script>
</head>
<body>
<div ng-app="testNgView">
<div ng-controller="routeReload">
View page <a ng-click="routeReloading();" href="#one">one</a> or
<a ng-click="routeReloading();" href="#two">two</a>
<div ng-view></div>
</div>
</div>
</body>
</html>
page1file.php:
<div ng-controller="testOne">
This is {{name}}
</div>
page2file.php:
<script type="text/javascript">
function testTwo($scope) {
$scope.name = "page two";
}
testTwo.$inject = ['$scope'];
testNgView.controller('testTwo',testTwo);
</script>
<div ng-controller="testTwo">
This is {{name}}
</div>
【问题讨论】:
-
控制台有错误吗?
-
我检查元素,它显示如下“这是{{name}}”
-
所以没有错误?
-
您确定
testNgView在您的第二页范围内吗? -
你在哪里定义控制器'testTwo'?
标签: javascript php jquery html angularjs