0

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.

3
  • you directive definition name is getData and your usage is my-directive? Commented Jul 22, 2015 at 5:10
  • @Chandermani it is mistake in type. Commented Jul 22, 2015 at 5:13
  • If you're going to use your fetched data in other places you should use a service or factory Commented Jul 22, 2015 at 6:43

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

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.