user3018350
user3018350

Reputation:

AngularJs directive one way data binding not working

I want to use one way data binding from controller to my directive, but I get undefined instead of real value.

Here is definition of directive scope

scope: {
    eventAdmin: '@',
    eventId: '@'
  }

Here is how I use directive.

<directive event-admin="{{eventAdmin}}" event-id="{{eventId}}"></directive>

And here is my link function

function directiveLink (scope, element, attrs) {
console.log(scope.eventId); //-> undefined

}

Upvotes: 0

Views: 838

Answers (1)

Royar
Royar

Reputation: 621

If the directive's name is "directive" and the containing controller's scope has the properties "eventAdmin" and "eventId" then this should be working.

I made a JSFiddle of your example: https://jsfiddle.net/Royar/qu55ujj5/4/

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
  <directive event-admin="{{eventAdmin}}" event-id="{{eventId}}"></directive>
</div>

JS:

var myApp = angular.module('myApp', []);
angular.module("myApp").controller("myCtrl", function($scope) {
  $scope.eventAdmin = "aaa";
  $scope.eventId = "bbb";

});
angular.module("myApp").directive("directive", function() {
  return {
    scope: {
      eventAdmin: "@",
      eventId: "@"
    },
    template: "{{eventAdmin}}",
    link: function(scope, element, attrs) {
      console.log(scope.eventAdmin); //working
    }
  };
});

Upvotes: 1

Related Questions