I'm using Anguljarjs 1.4x. I'm trying to pass a value from an external function back to a directive. I've done some similar things before, but for some reason the function parameter is not coming back to my controller.
Here is some working plunker code I had to pass an external function to a directive. What I want to do now is add in a single text parameter value.
index.html
<!DOCTYPE html> <html> <head> <script data-require="[email protected]" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <div id="app" ng-app="app"> <div ng-controller="mainCtrl"> <my-directive ctrl-fn="ctrlFn()"></my-directive> </div> </div> </body> </html> myPage.html
<div> <button ng-click='ctrlFn()'>Click Here</button> </div> script.js
var app = angular.module('app', []); app.controller('mainCtrl', function($scope){ $scope.count = 0; $scope.ctrlFn = function() { console.log('In mainCtrl ctrlFn!'); $scope.count += 10; console.log("count is: " + JSON.stringify($scope.count)); }; }); app.directive('myDirective', function() { return { restrict: 'E', scope: { 'ctrlFn' : '&' }, // template: "<div><button ng-click='ctrlFn()'>Click Here</button></div>", templateUrl: "./myPage.html", link: function(scope, element, attributes) { scope.ctrlFn(); console.log("In link!"); } }; }); When the app runs I get this: In mainCtrl ctrlFn!
count is: 10
In link!
After I click the button I get this: In mainCtrl ctrlFn!
count is: 20
As I said this all worked, until I added in a parameter. All I'm trying to do is add in a text param, but I'm getting an error when I tried this in myPage.html:
Error: cannot use 'in' operator to search for 'ctrlFn' in 'hello'
<button ng-click='ctrlFn('hello')'>Click Here</button> I added in a text value to my ng-click function and then created a param in the other places I have that function listed.
Any ideas as to how to do this?
<button ng-click='ctrlFn('hello')'>to<button ng-click="ctrlFn({$event:'hello'})">