简体   繁体   中英

How to get access to $route in meanjs controller

I am trying to get access to the current route since I added a title on it.

So the route looks something like

state('reports', {
    title: 'Reports',
    url: '/reports',
    templateUrl: 'modules/feeditems/views/reports.client.view.html'
}).

I want to get access to the title. so I can put it on my page Header. So in the Header controller I thought I could just get it off my

angular.module('core').controller('HeaderController', ['$rootScope', '$route', '$scope', 'Authentication', 'Menus',     
function($rootScope, $route, $scope, Authentication, Menus) {
        $scope.authentication = Authentication;         
        $scope.isCollapsed = false;
        $scope.menu = Menus.getMenu('topbar');
        $scope.$route = $route;
        console.log ('typeof($route) = ' + typeof($route));
        console.log ('typeof($route.current) = ' + typeof($route.current));

and I get the following error

Error: [$injector:unpr] Unknown provider: $routeProvider <- $route

so I add ngRoute

ApplicationConfiguration.registerModule('core', ['ngRoute']);

then I get the following error

Uncaught Error: [$injector:modulerr] Failed to instantiate module r2h due to: Error: [$injector:modulerr] Failed to instantiate module core due to: Error: [$injector:modulerr] Failed to instantiate module ngRoute due to: Error: [$injector:nomod] Module 'ngRoute' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

How am I supposed to include this properly? the meanjs way?

I think you are confused, MeanJS standard configuration uses Angular UI Router

For Angular UI Router

You need to angular-ui-router.js then include ui.router inside your app dependency

After that in configuration do register your state using $stateProvider

Syntax

app.config(function($stateProvider){

    $stateProvider.state('reports', {
        url: '/reports',
        templateUrl: 'modules/feeditems/views/reports.client.view.html'
    })

})

For adding title dynamically you could refer this SO Answer

Kindly refer to https://stackoverflow.com/a/28252931/4380266 . This has the solution to your problem.

Check the sequence you loaded your angular files. The sequence must be :

angular.js 
angular-route.js 
finally your angular scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM