Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using functions for controller in angularjs

I was using controllers like this

.controller("somename",function($scope,$http){
//some get function to fetch data
$scope.data = dataReturned;
$scope.$apply();
});

It was working fine. Then I wanted to use functions after reading johnpapa's blog and changed it to the one like below

.controller("somename",someNameController);
function someNameController(){
var someName = this;
//some get function to fetch data
    this.data = dataReturned;
    this.$apply();
};

but this did not work as this.$apply is not a function

When I added $scope(which is not recommended) it started working

 .controller("somename",someNameController);
    function someNameController($scope){
    var someName = this;
    //some get function to fetch data
        $scope.data = dataReturned;
        $scope.$apply();
    };

is it possible to eliminate the passing of $scope in function someNameController($scope) ?

like image 586
Vignesh Subramanian Avatar asked May 13 '26 19:05

Vignesh Subramanian


1 Answers

why do you want to skip the $scope? in order to get the reference of all the methods and variables in the controller you have to use the $scope .thanks


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!