17

I want to assign a javascript function to ng-click which name is sourced by a rest service.

<li ng-class="{ active: isActive('url')}" data-ng-repeat="menu in mb.data">
   <a href="{{menu.href}}" data-ng-click="{{menu.javascript}}">{{menu.label}}</a>
</li>

Sadly the angularjs parser throws an exception if one use {{ }} inside of ng-click. So I have tried several workarounds like using a callback function

  <a href="{{menu.href}}" data-ng-click="call(menu.javascript)">{{menu.label}}</a>

But none of my ideas succeeded. How can I assign a javascript functions name in ng-lick? Btw. the function itself is part of the $scope.

Edit- Here is the controller:

The "$menu" service is simply a get rest request. The request result is a json object and only holding string values. In the current issue the result for menu.javascript is "loginModal()".

.controller('HeaderController', function($scope, $http, ModalService, $menu, $routeParams) {
    $scope.loginModal = function() {
        console.log("modal", ModalService);
        // Just provide a template url, a controller and call 'showModal'.
        ModalService.showModal({
            templateUrl: "/modals/login.modal.html",
            controller: "LoginController"
        }).then(function(modal) {
            // The modal object has the element built, if this is a bootstrap modal
            // you can call 'modal' to show it, if it's a custom modal just show or hide
            // it as you need to.
            console.log(modal.element);
            modal.element.modal();
            modal.close.then(function(result) {
                console.log(result ? "You said Yes" : "You said No");
            });
        });
    };

    $menu.get($routeParams, function(data){
        $scope.menu = data;
    });
})

Edit 2:

Interestingly when I use {{menu.javascript}} then the correct string "loginModal()" is available in the DOM. But the angular parser stopped there due to errors.

5
  • Use simple data-ng-click="menu.javascript" Commented Feb 16, 2015 at 11:42
  • just use this without any bracket ng-click="yourFunctionOnScope()" Commented Feb 16, 2015 at 11:42
  • yes data-ng-click="menu.javascript" compiles but does not call the script ... Commented Feb 16, 2015 at 11:44
  • What I believe is you have to use $compile service in order to use ng-click working. still I'm not sure about it. Commented Feb 16, 2015 at 11:48
  • can you provide more info about menu.javascript function? Commented Feb 16, 2015 at 11:50

2 Answers 2

29

You can do something like this

HTML

  <div ng-controller="TodoCtrl">
      <button ng-click="callFunction('testClick')">CLICK ME</button>
  </div>

Controller

function TodoCtrl($scope) {

    $scope.callFunction = function (name){
        if(angular.isFunction($scope[name]))
           $scope[name]();
    }

    $scope.testClick = function(){
        alert("Called");
    }
}

Working Fiddle

Hope this could help you. Thanks.

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

7 Comments

I have tried this one. Sadly It only works if use use fixed Strings. Which means "testClick" can not be a value of varibale.
@KIC in your case it would be data-ng-click="callFunction('{{menu.javascript}}')"
yes, data-ng-click="callFunction(menu.javascrpt)" works, I forgot to remove the () in my rest response. thx!
if I need pass callFunction into this module as isolated scope
Isolated scope will only come into the picture when you have directive.. Not sure whats ypu question?
|
0

You can use the hashmap notation accessing the property of 'this' object (which, in this case, is $scope):

<li ng-class="{ active: isActive('url')}" data-ng-repeat="menu in mb.data">
   <a href="{{menu.href}}" data-ng-click="this[menu.javascript]()">{{menu.label}}</a>
</li>

Comments

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.