angularjsangularjs-directivecustom-directive

Angular Js ng-repeat with custom directive templateUrl


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.


Solution

  • 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>