0

I am using https://github.com/johnpapa/angular-styleguide convention.

Inside section Function Declarations to Hide Implementation Details

How to use function in side html when i bind it to this like:

function AvengersCtrl() {
    var vm = this;
    vm.activate = activate;


    function activate() {
        alert("activate");
    }
}

and use it in html:

<body ng-controller="AvengersCtrl">...
    <button ng-click="activate()"></button>
</body>

when i use scope instead of this it works

$scope.activate = activate;

2 Answers 2

2

You need to use what's known as controller as syntax:

<body ng-controller="AvengersCtrl as ctrl">...
    <button ng-click="ctrl.activate()"></button>
</body>
Sign up to request clarification or add additional context in comments.

Comments

1
<body ng-controller="AvengersCtrl as aveng">...
<button ng-click="aveng.activate()"></button>
</body>

if you don't specify an alias for the controller (as i have done with 'aveng') and you don't call your function with alias.function() syntax, angular will look for an 'activate()' function inside your global functions, not inside your AvengersCtrl object

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.