JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-controller="myController">
<input type="text" ng-model="myVar" /> -> {{myVar}}<br/>
<button ng-click="setToDirective()">Set testVar (aVar)</button>
<!-- Notice update to b-var -->
<my-directive directive-var="toDirective" b-var="testFunc(theVar)"> </my-directive>
</div>
JavaScript
var myApp = angular.module("myApp", []);
function myController($scope) {
$scope.myVar = null;
$scope.toDirective = {someString: ""};
$scope.setToDirective = function() {
$scope.toDirective = {someString: $scope.myVar};
console.log("Updated toDirective: ",$scope.toDirective);
}
$scope.testFunc = function(someObject) {
console.log("myController.testFunc: ",someObject);
};
}
myApp
.directive("myDirective", function() {
return {
restrict: 'E',
scope: {
directiveVar: '=',
bVar: '&'
},
controller: function($scope, $attrs) {
$scope.test = [];
$scope.$watch("directiveVar", function(newValue, oldValue, scope) {
if(angular.isDefined(newValue) && newValue !== null) {
console.log("myDirective.$watch: aVar has changed!");
newValue.fromDirective = true;
$scope.test.push(newValue);
// Calls delegated function.
//
// NOTE: You MUST pass an object map which contains the parameter as a key,
// and its value as the value
//
// example:
// .. b-var="testFunc(param1, param2)"></my-directive>
// $scope.bVar({param1: "etc", param2: "yup"});
$scope.bVar({theVar: newValue});
}
});
}
}
})
;