2

I'm using a custom directive in angular js with template url the link to code is here. The Problem is ngRpeat is not working with template url if I pass ngRepeat to the element then it does not work but if I pass in the template itself it works.

1
  • add fiddle with issue Commented Feb 17, 2017 at 6:51

2 Answers 2

2

Update:

Following is the code you've written in main.html

<search-results customers-d ="customers" ng-repeat="CM in customersD></search-results>

Following is the directive searchResults you've written:

myApp.directive('searchResults', function () {
    return {
        templateUrl: 'directives/search.html',
        scope: {
            customersD: '=',
        }
    }
});

Following is the main controller you've written:

myApp.controller('mainController', ['$scope', '$log', function($scope, $log) {
    $scope.customers = [{ name:'Rishabh'},{name:'Krishna'}]
}]);

And search.html is as follows:

<a href="#" class="list-group-item">
    <h4 class="list-group-item-heading"> hi </h4>
    <p class="list-group-item-text">{{CM.name}}</p>
</a>

Now things you are doing wrong:

  1. Missing closing quote in ng-repeat of main.html
  2. Trying to access customersD in main.html, while no array named customersD is defined in $scope of mainController.
  3. Trying to access CM in search.html (which is template of isolated scope directive). You can only have customersD in search.html

I think your understanding of scopes is not correct. It would be good if you read enough before asking questions here. :)

Previous Answer: You are missing closing quote in ng-repeat and using wrong variables Do as follows :

<search-results customers-d ="CM" ng-repeat="CM in customers"></search-results>
Sign up to request clarification or add additional context in comments.

3 Comments

added that quote and ur variable still not showing name and aslo plz explain the reasons to change variable
Hey Pranav Thanks For Your Reply ! Your Second Point Says Trying to access customersD in main.html, while no array named customersD is defined in $scope of mainController. But Thats why I have defined customersD which is accessing customers object
customers-d ="customers" passes customers object to directive and can be accessed in directive using customersD but the array you are trying to ng-repeat is customersD which is not present in scope of mainController. So no loop is executed and nothing is displayed.
0

main.html

<div class="list-group">
    <search-results customers-d ="customers" ng-repeat="CM in customers"></search-results>
</div>

Directive changes in app.js

myApp.directive('searchResults', function () {
return {
    restrict : "AE",
    templateUrl: 'directives/search.html'
}
});

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.