0

I'm having some problems trying to make http calls inside the same javascript controller which it gives me the error: 'function is not defined'

If I move the function to the outside of controller, i can't use $http dependency.. So here's my problem...

What should i use?

I'm trying to call this function through an onclick event on a div that I created dinamically.

I've already tried this but it also does not work:

function MyController($scope,$http,$resource) {
    function activateSmartcase(deviceId) {
};
}

Thanks

4
  • You call this method from ng-click directive? Commented Jul 25, 2017 at 15:34
  • 2
    I think it would be helpful if you showed us more code Commented Jul 25, 2017 at 15:35
  • Please add the HTML that is giving you the problem. Commented Jul 25, 2017 at 19:07
  • function declarations inside controllers are private to those controller. To invoke a function from the ng-click directive, the function must be attached to $scope or the this context depending on how the controller is invoked, either vanilla syntax or controllerAs syntax. Commented Jul 25, 2017 at 19:13

2 Answers 2

1

If you call this method from ng-click directive than it should be attached to $scope

$scope.activateSmartcase=function(deviceId){

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

1 Comment

i'm using onclick.. when i use ng-click it doesn't work.. and i have the angular libraries.. i don't know what's happening lol
0

This should work

(function(){

    'use strict';

    angular
        .module('app')
        .controller('NameOfTheController', Controller);

        function Controller($scope,$http,$resource){
            const vm = this;
            vm.activateSmartcase = function(){
            /*code block*/
            }
        }

})();

4 Comments

where should I put the function that'll call with onclick? thanks :)
it doesnt work.. I think you didnt understand my problem.. I've already have a controller.. can i create another one? I think i don't.

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.