2

I am using the following way to use $scope variable ({{func}}() in this case) as function name in ng-click.

<button type="button" ng-click="{{func}}()">Call {{func}}</button></pre>

This works in angularjs-1.2.0rc3. See working plunkr here

Any future version from > 1.2.0rc3 throw this error

What's changed? How can I use the above syntax in current angular version?

2 Answers 2

7

Ok first of all I do not recommend such a usage for ng-click because angularjs itself do not support this, but if you still want to use it such a way here is your solution...

<button type="button" ng-click="$eval(functionName)()">...</button>

where

$scope.f1 = function() {
    ...
};
//name of function as a string
$scope.functionName = "f1";

this is what your are looking for and here is your PLUNKER example...

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

1 Comment

Got it. I guess converting to directive is the right way. Yes it's not a recommended way. But it used to work and it stopped working.
0

All I did was append scope to both variables

<form name="angular" ng-controller="Ctrl">
    <button type="button" ng-click="{{scope.func}}()">
        Call {{func}}
    </button>
    <label>Status: {{scope.status}}</label>

http://jsfiddle.net/bebold/TmKLY/1/

I wouldn't advise going this route for dynamic variable change, a better choice would be to create a directive and do the binding within the template:

A great explanation can be found HERE.

1 Comment

I don't think scope needs to be appended. Try replacing 1.2.0rc3 version with 1.2.1 or any other version and you will see the error in console.

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.