0

I have a custom directive:

.directive('test', function () {

    return {
        scope: {},

        link: function (scope, element, attr) {

            scope.$parent.$watch(attr.selectedItem, function(newValue, oldValue){
                scope.selectedItem = newValue;
            });
         }
}

This will one way bind my directive's scope's selectedItem property to the value set in the attribute as such

<div test selectedItem="thePropertyOnTheController"></div>

But what if I want to two way bind? Is there an easy way to set this up without $watch'ing the directive's scope's selectedItem property and $parse'ing the attr.selectedItem expression and calling assign witht he parsed expression on scope.$parent?

1
  • 1
    Use scope: { selectedItem: "=" } - it's all in the isolate scope documentation Commented Apr 15, 2015 at 12:42

2 Answers 2

1

$scope.thePropertyOnTheController might have some value like "Hello"

HTML

   <div ng-repeat="photosets in userPhotoSetList">
     <photosets photosetsarray="photosets.photosetDetail">
  </div>

script :

.directive('photosets', function () {
  return {
      scope: {
               photosetslist : "=photosetsarray"
             },
      link: function (scope, element, attr) {
       console.log(scope.photosetslist);
       //"Hello" is output
     }
}

If you see photosetsarray="photosets.photosetDetail"" photosetsarray and

            scope: {
               photosetslist : "=photosetsarray" **//this name is same as assignee attr**
             },

leftside variable name in html must = right side variable name in directive

Sign up to request clarification or add additional context in comments.

4 Comments

What if I want to do this programmatically in code? Any easy way?
leftside variable name in html must = right side variable name in directive
Here's the reason I ask - this doesn't work: scope: { options: {imageIndex: '=popupImagesIndex'} }....so my idea is to accomplish the binding in the link function.
imageIndex : '=popupImagesIndex' and photosetslist : "=photosetsarray" the left side assign photosetsarray must equal to right side variable of directive scope. scope: { photosetslist : "=photosetsarray" //this name is same as assignee attr }, AND also I suggest you to take variable as imageindex not like imageIndex Please check Edited answer
0

Be careful with variable naming in these situations. Binding to an attribute that is declared as camel case in the directive cannot be accessed as such from the DOM.

.directive('test', function () {
  return {
      scope: {
               item : "=selectedItem"
             },
      link: function (scope, element, attr) {
         //do some stuff
     }
}

So to correctly bind this attribute to a variable on the controller:

<div test selected-item="thePropertyOnTheController"></div>

1 Comment

I'm not sure what you're saying

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.