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?

2
  • 1
    you need to use $timeout(function(){alert($scope["cronos-dataset"]);}) inside controller Commented Mar 4, 2015 at 14:22
  • Can you crate a plnkr/fiddle? Your code should work fine. plnkr.co/edit/2gdUSFoWXUTJAONqGLqn?p=preview Commented Mar 4, 2015 at 14:35

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

5

Make a link function instead:

app.directive('cronosDataset',[function() {
  return {
    scope: {},
    restrict: 'A',
    link: function (scope, elem, attrs) {
        alert(attrs.cronosDataset);
    }

2 Comments

he does wanted to call inside controller not in link function?
That alerts! Thanks but I need to call it inside a controller!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.