Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS显示模块提供的消息_Angularjs - Fatal编程技术网

AngularJS显示模块提供的消息

AngularJS显示模块提供的消息,angularjs,Angularjs,我正在开发一个用于教育目的的平均堆栈应用程序,在尝试处理角度模块和控制器时遇到了一个问题 这是我的modules.js代码 'use strict'; var myApp = angular.module("myApp", ["ngRoute"]); myApp.config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(false).hashPrefix(''); $rou

我正在开发一个用于教育目的的平均堆栈应用程序,在尝试处理角度模块和控制器时遇到了一个问题

这是我的modules.js代码

'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}}