1

I have a controller and element directive:

        ngModule
            .controller('summaryCtrl', [ '$scope', '$http', function($scope, $http){
                $scope.loaded = false;
                $http
                    .get('some/item/'+itemId) //how do I get this itemId
                    .success(function(data){
                        $scope.data = data;
                        $scope.loaded = true;
                    })
                    .error(function(data){
                        //TODO
                    });
            }])
            .directive('cpSummary', function(){
                return {restrict: 'E', templateUrl: 'some/path.html'};
            });

and I want to use the directive something like this:

<cp-summary item-id="{id}" ng-controller="summaryCtrl"></cp-summary>

the item-id attribute get's set by the parent controller that is rendering the cp-summary elements in a ng-repeat. So I'd just like to know if it's possible to get the item-id attribute value inside the summaryCtrl.

2
  • If I understand you want just pass itemId to summaryController yes? This controller well be used only with this directive? Commented Aug 10, 2015 at 12:27
  • Look at this: codepen.io/anon/pen/jPJYdX Commented Aug 10, 2015 at 12:53

3 Answers 3

1

You should use a controller for your directive. and then you can pass the item-id to the directive controller and do the stuff.

app.directive('cpSummary', function(){
            return {
                restrict: 'E',
                templateUrl: 'some/path.html',
                scope: {
                    item_id: '=itemId'
                },
                controller: ['$scope','$http',function($scope,$http) {
                    $http
                         .get('some/item/'+$scope.item_id)
                         .success(function(data){
                             $scope.data = data;
                             $scope.loaded = true;
                         })
                         .error(function(data){
                            //TODO
                         });
                }]
            };
        });
Sign up to request clarification or add additional context in comments.

2 Comments

this solution will work if attribute is item-id="id"
Is it possible to pass an array to the controller: property? like ['$scope', '$http', function($scope, $http){/*...*/}] otherwise when the code gets minified $scope and $http will become a, b.
1

As you wanted to pass id to you directive then you should include that variable inside directive isolated scope. It should be {{id}} instead of {id} & then use @ inside your directive. @ is for one way binding. Also assign the controller from directive.

Markup

<cp-summary item-id="{{id}}"></cp-summary>

Directive

.directive('cpSummary', function(){
    return {
       restrict: 'E', 
       templateUrl: 'some/path.html',
       scope: {
          item_id: '@itemId'
       },
       controller: 'summaryCtrl'
    };
});

Comments

0
var app = angular.module("App", []);

app.directive("myDirective", function() {
    var itemFunction = function(scope, element, attributes) {
        scope.item-id= attributes["myDirective"];
  };

  return {
    restrict: "A",
    template: "<p></p>",
    link: itemFunction
  };
});

Comments

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.