Mindaugas
Mindaugas

Reputation: 1173

Controller data not passed to directive

I started looking into angular and cannot seem to figure out why my data is not passed to my directive. I have code here: https://plnkr.co/edit/ONwYevQ4NbvBVjTwARHl?p=preview

My Code: app.js:

var app = angular.module('mjApp', []);

app.controller('MainController', ['$scope', '$http', function ($scope, $http) {
  $scope.name = "m.jacionis";
  $scope.avatar = null;
  $scope.fetchData = function(){
    var callUrl = 'https://api.github.com/search/users?q=' + $scope.name;
    $scope.avatar = "http://images.clipartpanda.com/sad-face-clipart-black-and-white-9c4eqRyyi.png";

    $http({
      method: 'GET',
      url: callUrl
    }).then(function successCallback(response) {
      $scope.avatar = response.data.items.length > 0 ? 
        response.data.items[0].avatar_url : $scope.avatar;
      console.log($scope.avatar);
    }, function errorCallback(response) {
      console.log('avatar stays the same');
    });
  };
}]);

app.directive("mjDirective", function(){
  return {
    restrict: "EA",
    templateUrl: 'template.html',
    scope: {
      name: "=name",
      avatar: "=avatar"
    }
  };
});

index.html:

<!DOCTYPE html>
<html ng-app="mjApp">

  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div class="parent" ng-controller="MainController">
      <div class="line">
        <input type='text' ng-model='name' />
        <input type="button" ng-click="fetchData()" value="Submit User Name" />
      </div>
      <mj-directive name=name userAvatar=userAvatar></mj-directive>
    </div>
  </body>

</html>

template.html:

<div class='line'>
  <p>Name : <strong>{{name}}</strong></p>
  <img class="avatar" src={{avatar}}/>
</div>

name value is passed, but avatar value which I get when I fetch data isn't. I cannot seem to figure out, why it is like that. Any ideas or suggestions would help a lot.

Upvotes: 0

Views: 200

Answers (2)

Peter_Fretter
Peter_Fretter

Reputation: 1165

I think you need to wrap the name and avatar in strings when you use them in HTML.

<mj-directive name="name" userAvatar="userAvatar"></mj-directive>

Also inside the directive you should have:

scope: {
    user: "=name"
    avatar: "=userAvatar"
}

Upvotes: 1

Sravan
Sravan

Reputation: 18647

I think you have taken wrong name userAvatar instead of avatar since you binded avatar

your bindings,

  scope: {
      name: "=name",
      avatar: "=avatar"
    }

So, you have to take name and avatar in directive.

  <body>
    <div class="parent" ng-controller="MainController">
      <div class="line">
        <input type='text' ng-model='name' />
        <input type="button" ng-click="fetchData()" value="Submit User Name" />
      </div>
      <mj-directive name=name avatar=avatar></mj-directive>
    </div>
  </body>

change directive,

<mj-directive name=name avatar=avatar></mj-directive>

Upvotes: 1

Related Questions