0

I want to pass an object into my controller so I will have a contextual set of elements available at the time the controller fires. I'm basically trying to configure the meta information of each controller in my config file for my routes.

For example. (I want to create a meta and have it injected into the controller somehow...

//In config.js
routes["/dashboard"] =  { 

          controller: 'controllers.DashboardController', 
           templateUrl: 'templates/dashboard.html',
           meta: {
                title: "My Title",
                description: "My Desc"

            }

         };


//In dashboard.js

controllers.DashboardController = function ($scope, $location, Page) {
    'use strict';
    Page.setTitle(meta.title); // Pull this from content coming in
    Page.setDescription(meta.description);
Tim
  • 41,901
  • 18
  • 127
  • 145
chrislhardin
  • 1,747
  • 1
  • 28
  • 44

2 Answers2

1

this is what I ended up with... It works Elegantly...

In my config

  //Route configurations
routes["/dashboard"] =  { 

         controller: 'controllers.DashboardController', 
         templateUrl: 'templates/dashboard.html', 
         meta: {
           title: "Dashboard",
           description: 'This is my description'
         }

 };

application code

 application.config(['$routeProvider', '$locationProvider','cfg', function AppConfig($routeProvider, $locationProvider, cfg, $routeParams) {

     $locationProvider.html5Mode(true).hashPrefix('!');

 console.log(cfg);
 for (var route in cfg.routes)  
     $routeProvider.when(route, cfg.routes[route]);

...

application.run(['$rootScope','Page', function($rootScope, Page) {


    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {

        Page.setTitle(current.$$route.meta.title); // Pull this from content coming in
        Page.setDescription(current.$$route.meta.title);
    });
}]);

...

 <title ng-bind="'my title &mdash; ' + Page.title()">My title </title>
    <meta name="description" content="{{Page.description()}}"/>
chrislhardin
  • 1,747
  • 1
  • 28
  • 44
0

I think that the route resolve function will do the trick.

From the docs:

https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Question thread with examples:

Delaying AngularJS route change until model loaded to prevent flicker

Community
  • 1
  • 1
haimlit
  • 2,572
  • 2
  • 22
  • 26