1

How to pass a fields variable to directive in ng route configuration, Or in another phase?

.when('/test',{template:"<my-directive fields=field></my-directive>"})

How to assign param to directive in routing phase?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Mhd Wael Jazmati
  • 636
  • 9
  • 18
  • Could you be more specific? Like which attributes you want to set? Could you show us your controller if any? – Emile Bergeron Jul 22 '16 at 20:09
  • i need to assign fields to directive as i explain above, my question is : from where i will bring fields parameters ? am i should use resolve to return fields or from directive controller i should get fields ? – Mhd Wael Jazmati Jul 22 '16 at 20:13
  • Normally, you would prepare the data (e.g. the `$scope`) inside the controller and that data is then accessible from the template. – Emile Bergeron Jul 22 '16 at 20:15
  • Thanks for your interesting, but i need same controller to use with multiple data, so that i don't want to use $scope in controller, i need to use same controller with multiple params? – Mhd Wael Jazmati Jul 22 '16 at 20:17
  • I'll need more details to help you further. – Emile Bergeron Jul 22 '16 at 20:28

1 Answers1

1

Make sure to include your directive when defining the module dependencies:

var app = angular.module('sampleApp', [
    'ngRoute',
    'myDirective' // here, you need to include your directive module
]);

Then, define your routes:

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', { template: "This is the default Route" })
        .when('/test', {
            template: '<my-directive fields="field"></my-directive>',
            controller: 'testController'
        })
        .otherwise({ redirectTo: '/' });
}]);

And a controller:

app.controller('testController', ['$scope', function($scope) {

    $scope.field = { your: "data here" };

}]);
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129