AngularJS显示模块提供的消息
我正在开发一个用于教育目的的平均堆栈应用程序,在尝试处理角度模块和控制器时遇到了一个问题 这是我的modules.js代码AngularJS显示模块提供的消息,angularjs,Angularjs,我正在开发一个用于教育目的的平均堆栈应用程序,在尝试处理角度模块和控制器时遇到了一个问题 这是我的modules.js代码 'use strict'; var myApp = angular.module("myApp", ["ngRoute"]); myApp.config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(false).hashPrefix(''); $rou
'use strict';
var myApp = angular.module("myApp", ["ngRoute"]);
myApp.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(false).hashPrefix('');
$routeProvider
.when("/test1", {
templateUrl : "views/test1.html",
controller: "TestController",
message: "Test 1"
})
.when("/test2", {
templateUrl : "views/test2.html",
controller: "TestController",
message: "Test 2"
});
});
这是我的test.controller.js代码
myApp.controller('TestController', ['$scope', function($scope) {
// some code
}]);
我有这个代码段的test1.html和test2.html文件
<div ng-app="myApp">
<div ng-controller="TestController">
<span>{{ message }}</span>
</div>
</div>
{{message}}
如何分别在test1.html和test2.html中显示modules.js文件中定义的消息“Test 1”和“Test 2”?获取控制器中$state对象的引用:
myApp.controller('TestController', ['$scope', '$state', function($scope, $state) {
$scope.myState = $state;
}]);
myApp.controller('TestController', ['$scope', '$route', function($scope, $state) {
$scope.myRoute = $route;
}]);
在您的视图中,将此{{myState.current.message}}
你可以看看
编辑:
事实上,这可能只在您使用ui路由器时起作用,而您似乎不使用它
编辑2:
因此,如果要使用ngRoute
执行此操作,需要在控制器中获取对$route
的引用:
myApp.controller('TestController', ['$scope', '$state', function($scope, $state) {
$scope.myState = $state;
}]);
myApp.controller('TestController', ['$scope', '$route', function($scope, $state) {
$scope.myRoute = $route;
}]);
并将其与以下内容绑定在您的视图中:
{{myRoute.routes["test1"].message}}