0

I am new to angular js and still working on things. I would like to ask if it's possible to initiate ng-click variables (which are functions.) in angular js? Here is as snippet of my code

<div data-ng-controller="DeathRayController">
    <button data-ng-click="toggleMenu()">Toggle Menu</button>
    <ul ng-show="menuState.show">
        <li data-ng-repeat="menu in menuList"><a href="" data-ng-click="menu.mode">{{ menu.value }}</a></li>
    </ul>
</div>

and here is my controller using angularjs:

var showHide = angular.module('showHide', []);

showHide.controller('DeathRayController', function ($scope) {
// the idea is to display this on the <a ng-click="menu.mode">
    $scope.menuList = [
        { mode: 'stun()', value : 'Stun' },
        { mode: 'disinegrate()' ,value : 'Disinegrate' },
        { mode: 'erase()', value : 'Erase' },
    ];
    $scope.menuState = { show: false };
    $scope.toggleMenu = function() {
        $scope.menuState.show = !$scope.menuState.show;
    };
    $scope.stun = function() {
        alert('Snorlax is stunned!');
    };
    $scope.disinegrate = function() {
        alert('Charizard used Disinegrate!');
    };
    $scope.erase = function() {
        alert('erased Sleep skill');
    };
});

If it's still unclear please do tell me.

2
  • What do you mean by "initiate" ng-click? Commented May 7, 2014 at 4:15
  • You need to use data-ng-click="menu.mode()", but the mode property should point to a function. In your case i see it as a string variable. Commented May 7, 2014 at 4:20

2 Answers 2

1

In your controller, when you define your menuList array, do not set mode to a string, but instead to the actual function

$scope.menuList = [
    { mode: $scope.stun, value : 'Stun' },
    { mode: $scope.disinegrate ,value : 'Disinegrate' },
    { mode: $scope.erase, value : 'Erase' },
];

Then in your markup:

data-ng-click="menu.mode()"

here is a link to working code

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

Comments

0

Define functions before assigning them to object.Assign them as

$scope.menuList = [
    { mode: $scope.stun, value : 'Stun' },
    { mode: $scope.disinegrate ,value : 'Disinegrate' },
    { mode: $scope.erase, value : 'Erase' },
]; 

and use them in html as

<ul>  
 <li data-ng-repeat="menu in menuList"><a href="" data-ng-click="menu.mode()">{{ menu.value }}</a></li>
</ul>

View:

<div data-ng-controller="DeathRayController">
  <button data-ng-click="toggleMenu()">Toggle Menu</button>
  <ul ng-show="menuState.show">
    <li data-ng-repeat="menu in menuList"><a href="" data-ng-click="menu.mode()">{{ menu.value }}</a></li>
  </ul>
</div>

controller:

var showHide = angular.module('showHide', []);

showHide.controller('DeathRayController', function ($scope) {
// the idea is to display this on the <a ng-click="menu.mode()">
$scope.stun = function() {
    alert('Snorlax is stunned!');
};
$scope.disinegrate = function() {
    alert('Charizard used Disinegrate!');
};
$scope.erase = function() {
    alert('erased Sleep skill');
};
$scope.menuList = [
    { mode: $scope.stun, value : 'Stun' },
    { mode: $scope.disinegrate ,value : 'Disinegrate' },
    { mode: $scope.erase, value : 'Erase' },
];
$scope.menuState = { show: false };
$scope.toggleMenu = function() {
    $scope.menuState.show = !$scope.menuState.show;
};    

});

Fiddle: http://jsfiddle.net/rVCV2/1/

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.