0

FIDDLE

I have created a directive :-

return {
        restrict: 'EAC',
        scope: {
            statesActive: '='
        },            
        link: function (scope, element, attrs) {
        var stateData = scope.statesActive.states; // Don't get data here
        ....
}

My Html :-

<gt-map states-active="states"></gt-map>

Controller :-

$scope.statesList = function () {
        var query = {
            status: 'active'
        }

        StatesList.query(query).$promise.then(function (states) {
            $scope.states = states;
        });
    };

I get data in states like this :-

$scope.states = "{states:[{ 'state' : 'CA', 'color' : '#61c419']}";

How to get the Controller scope value in directive?

-----Added-----

This is what i see in console.log(scope) in my directive :-

enter image description here

3
  • 1
    Try $scope.states = JSON.parse('{"states":[{ "state" : "CA", "color" : "#61c419"}]}') Commented Apr 10, 2014 at 5:11
  • $scope.states is string in controller. change it as $scope.states = {states:[{ 'state' : 'CA', 'color' : '#61c419'}]}; Commented Apr 10, 2014 at 5:18
  • @Alborz I get data from my mondogb...see edited question Commented Apr 10, 2014 at 5:22

6 Answers 6

1
return {
        restrict: 'EAC',
        scope: {
            statesActive: '='
        },            
        link: function (scope, element, attrs) {
        var stateData = angular.fromJson(attrs.statesActive).states; // Probably, your "states" model here
        ....
}
Sign up to request clarification or add additional context in comments.

6 Comments

When i console log attrs.statesActive i get states
And what is your problem? :)
i should get scope.states value i.e. {states:[{ 'state' : 'CA', 'color' : '#61c419'}]}.... but i only get string states.
Rly interesting, checking out.
access "scope.statesActive".
|
1

A slight tweak to the fiddle was referenced in a comment to another answer that now shows the retrieval of states within the directive's link function via its scope:

link: function (scope, element, attrs) {            
    var stateData = $parse(scope.statesActive)();
    console.log(stateData);
}

by stating:

scope: { statesActive: '=' }

you are binding your directive's scope to the attribute named states-active so all you have to do reference it inside the directive is to use

scope.statesActive

this will get the value as a string, you can use the $parse service to get the value of your object

1 Comment

I apologize, the orig fiddle was using an alert which shows the value as a string, but by using parse you can get the value as an object, see the updated fiddle, and note that you need to inject in $parse to your directive
1

You have mistakes in $scope.states. It must be an object, not a string.

Following code works

JS

angular.module('myApp', []);
angular.module('myApp').directive('gtMap', 
    function() {
      return {
          restrict: 'EAC',
          scope: {
              statesActive: '='
          },            
          link: function (scope, element, attrs) {
              var stateData = scope.statesActive.states;
              // Don't get data here
              console.log(scope.statesActive);
          }
       }
    }
  );

angular.module('myApp').controller('MyCtrl', 
    function($scope) {
        // If states is a string got from database, use angular.fromJson(json) to parse. 
        // $scope.states = angular.fromJson(response); 
        $scope.states = {states:[{ 'state' : 'CA', 'color' : '#61c419'}]};

    });

HTML

<div  ng-app="myApp">
    <div ng-controller="MyCtrl">
            <gt-map states-active="states"></gt-map>
    </div>
</div>

Check out this jsfiddle http://jsfiddle.net/fizerkhan/69Pq9/1/

Comments

0

Try using this to access your scope.

return {
    ...
    link: function (scope, element, attrs) {
        ....
        scope.$parent.$parent.YourVariable;
    }
};

And I think you need to change your HTML to this:

<gt-map states-active="states"></gt-map>

1 Comment

Tried this but..No Luck..!
0

inject $parse in your directive.... try this...

 angular.module('myApp').directive('gtMap', ['$parse',
    function($parse) {
      return {
          restrict: 'EAC',

          link: function (scope, element, attrs) {
              var fieldGetter = $parse(attrs.statesActive);
              console.log(fieldGetter)
                var field = fieldGetter(scope);
              //var stateData = scope.statesActive.states;
              // Don't get data here
              console.log(field)
          }
       }
    }
  ]);

2 Comments

This is what i get in my console for fieldGetter : - function (self, locals) { return (getter(self, locals)); }
dont use isolate scope : scope:{statesActive: '='} and comment out the 'console.log(fieldGetter)'...sorry i forgot to do that
0

scope.statesActive is an array so you need to access it as follows

var stateData = scope.statesActive[0].states;

2 Comments

It gives me error in console....TypeError: Cannot read property 'states' of undefined

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.