Hadi
Hadi

Reputation: 17289

Angularjs directive get data from url

i define a directive in angularjs that have a url to fetch data from data base. i display part of code. my directive is :

app.directive('getData', function() {

return {
 restrict: 'E',
  scope: {
    url: '@',
    suggestion: '=data'
   },
  controller: ['$scope','$http', function($scope,$http){
    $http.get($scope.url)
      .success(function (data) {
        console.log(data);// display data 
        $scope.Items = data;
      });
   })
  template:
           <li\
          suggestion\
              ng-repeat="suggestion in suggestions track by $index"\
          </li>\
}

now when i create a directive such as this in html

<get-data url="/public/getEmployee" data="Items"></get-data>

but no data display in it, while same code in directive controller be in an angularjs controller work correctly.

Upvotes: 0

Views: 374

Answers (1)

Chandermani
Chandermani

Reputation: 42669

You are calling the http request inside the directive and getting the data inside the directive, so you do not need to pass the suggestion variable in scope.

Directly use Items array in the template item in Items track by $index.

Isolated scope variable passed in scope property are used to get data from external dependencies.

And your directive usage should be

<get-data url="/public/getEmployee"></get-data>

Upvotes: 1

Related Questions