0

I've read lot of question with same text but none helped me, so I ask a new one.

I have a ng-app, ng-controller.

<div ng-app='mainApp'>
<div ng-init='period=".$_REQUEST['period']."' ng-controller='debtController'>

Then, some lines bellow, define them.

<script type='text/javascript'>
    var app = angular.module('mainApp', []);                               
        app.controller('debtController', function(){
            openDebt = function(period){
                console.log(period);
            }
    });
</script>

And last but not least, a ng-click that calls the function defined.

<a href="#" ng-click="openDebt('{{period}}')" />+</a>

So, if I click the '+' with the ng-click, nothing happens. But if I check the HTML there, and copy the text inside the ng-click, and then paste it in the console, it works. Why the function is not being called?

Anyone got any ideas?

1
  • see the code i have for the script. Commented Feb 26, 2016 at 18:58

3 Answers 3

1

Best approach

Notice the assyntax. This create an alias for the controller

<div ng-init='period=".$_REQUEST['period']."' ng-controller='debtController as debt'>

change the ng-click asociated to controller alias

<a href="#" ng-click="debt.openDebt('period')" />+</a>

In the controller code using this for associate the controller with the html

var app = angular.module('mainApp', []);                               
        app.controller('debtController', function(){
            this.openDebt = function(period){
                console.log(period);
            }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply pass the variable without an expression

Change From:

<a href="#" ng-click="openDebt('{{period}}')" />+</a>

To:

<a href="#" ng-click="openDebt(period)" />+</a>

4 Comments

didn't know that, thanks. Althought that doesn't solve the problem
what is the issue now?
The same, the function is not called but the function works... And if I copy th ng-click text in console it works.
you need to inject scope or create an alias, see how i am injecting scope before using scope. function($scope)
0

Okay first embed the openDebt to scope inside the controller: $scope.openDebt. Second while in ng-click, you don't need handlebar to pass the value.

<script type='text/javascript'>
    var app = angular.module('mainApp', []);                               
        app.controller('debtController', function($scope){
           $scope.openDebt = function(period){
                console.log(period);
            }
    });
</script>

HTML:

<a href="#" ng-click="openDebt(period)" />+</a>

PLUNKER EXAMPLE: http://plnkr.co/edit/OyhBWkrREtt2JT2IWuVN?p=preview

7 Comments

app.controller('debtController', function($scope){
see there function($scope)
If $scope is giving you error, perhaps you are minifying it, and breaks it, look how to inject correctly for minified js in angularjs: docs.angularjs.org/guide/di
Did that but console sends error with injector problem and stops angular from working
see the plunker. i said you using alias can also solve the problem :) Neway cheers that it is solved.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.