0

I have the following code:

<div id='parent'>
   <div id='child1'>
      <my-select></my-select>
   </div>
   <div id='child2'>
      <my-input></my-input>
   </div>
</div>

I also have two directives which get some data from the data factory. I need the two directives to talk to each other such that when a value in select box is changed the input in changes accordingly.

Here's my two directives:

.directive("mySelect", function ($compile) {
    return {
        restrict: 'E',
        scope:'=',
        template: " <select id='mapselectdropdown'>\
                        <option value=map1>map1</option> \
                        <option value=map2>map2</option> \
                    </select>'",
        link: function (scope, element, attrs) {
            scope.selectValue = //dont konw how to get the value of the select
        }
    };
})
.directive("myInput", function($compile) {
    return {
        restrict: 'E',
        controller: ['$scope', 'dataService', function ($scope, dataService) {
            dataService.getLocalData().then(function (data) {
                $scope.masterData = data.input;
            });
        }],
        template: "<input id='someInput'></input>",
        link: function (scope, element, attrs) {
            //here I need to get the select value and assign it to the input 
        }
    };
})

This would essentially do the onchange() function that you can add on selects. any ideas?

dting
  • 38,604
  • 10
  • 95
  • 114
Sean D
  • 356
  • 5
  • 20

3 Answers3

1

You could use $rootScope to broadcast a message that the other controller listens for:

// Broadcast with
$rootScope.$broadcast('inputChange', 'new value');

// Subscribe with
$rootScope.$on('inputChange', function(newValue) { /* do something */ });

Read Angular docs here

knation
  • 406
  • 3
  • 12
  • Using rootScope this ways is considered bad practice. You can actually use my-select as parent directive, and (optionally) requires it in my-input directive. Your selection state is simply a controller variable in parent directive – ABOS Apr 12 '15 at 12:46
0

Maybe transclude the directives to get access to properties of outer scope where you define the shared variable ?

What does this transclude option do, exactly? transclude makes the contents of a directive with this option have access to the scope outside of the directive rather than inside.

-> https://docs.angularjs.org/guide/directive

0

After much research this is what worked...

I added the following:

.directive('onChange', function() {    
        return {
            restrict: 'A',
            scope:{'onChange':'=' },
            link: function(scope, elm, attrs) {            
                scope.$watch('onChange', function(nVal) { elm.val(nVal); });            
                elm.bind('blur', function() {
                    var currentValue = elm.val();                
                    if( scope.onChange !== currentValue ) {
                        scope.$apply(function() {
                            scope.onChange = currentValue;
                        });
                    }
                });
            }
        };        
    })

Then on the element's link function I added:

link: function (scope, elm, attrs) {
    scope.$watch('onChange', function (nVal) {
       elm.val(nVal);
    });
}

Last added the attribute that the values would get set to in the scope:

<select name="map-select2" on-change="mapId" >
Sean D
  • 356
  • 5
  • 20