0

I want to configure my directive module through my app level controllers. Plunker

index.html

<div ng-controller="App">
  <foodz index="index"></foodz>
</div>

app.js

angular.module('app', ['foodz']).
  controller('App', ['$scope',function($scope){
    $scope.index = 1;
  }]);

foodz.js

angular.module('foodz', []).
  controller('foodzController', ['$scope',function($scope){

    //Data is coming in through external API
    $scope.$on('foodzFetched', function(e,d) {
      $scope.foodz = d;
    });

    //Lets say data to looks like:
    //[{"name":"banana"},{"name":"smoothy"}]
  }]).
  directive('foodz', function() {
    return {
      restrict: 'E',
      scope:{
        index: '@'
      },
      replace: true,
      controller: 'foodzController',
      templateUrl: 'foodzTemplate.html',
      link: function(scope, controller) {}
    };
});

foodzTemplate.html

<div ng-controller="foodzController">
  <span>
    {{foodz[index].name}}
  </span>
</div>

So in this example, I am trying to pass the index through my app level controller into an attribute of my directive element which also has its own controller.

What am I doing wrong here?

1
  • What is the problem you are facing. Can you try to create a fiddle. Commented Sep 9, 2013 at 16:26

1 Answer 1

2

Try to make scope:{index:'='}

Look at Directive Definition Object

I think you are receiving index='index' (as string) in your controller. Using = it will get the value from parent controller.

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

1 Comment

+1 Thank you this was just what I needed. I should have read docs closer haha.

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.