8

I want to get a value straight from an attribute directive:

 <form cronos-dataset="People as p">
     Form Content
 </form>

In my JS I tried:

app.directive('cronosDataset',[function() {
  return {
    restrict: 'A',
    controller: 'CronosGenericDatasetController',
    scope: {
        "cronos-dataset" : '@'
    }
  };
}])

.controller("CronosGenericDatasetController",['$scope', function($scope) {
    alert($scope["cronos-dataset"]);
}]);

I want to alert "People as p" string but I get undefined. Is that right path or should I go thorough a different approach?

nanndoj
  • 6,580
  • 7
  • 30
  • 42

2 Answers2

8

You are supposed to have camelCase in the scope declaration

app.directive('cronosDataset',[function() {
  return {
    restrict: 'A',
    controller: 'CronosGenericDatasetController',
    scope: {
        cronosDataset : '@'
    }
  };
}])

Here is a demo to see different variations http://plnkr.co/edit/G6BiGgs4pzNqLW2sSMt7?p=preview

HarryH
  • 1,058
  • 7
  • 12
5

Make a link function instead:

app.directive('cronosDataset',[function() {
  return {
    scope: {},
    restrict: 'A',
    link: function (scope, elem, attrs) {
        alert(attrs.cronosDataset);
    }
m0meni
  • 16,006
  • 16
  • 82
  • 141