3

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) ?

8
  • Err, why is taking $scope as an argument not recommended? Because it breaks on minification (for which, you can use the array or $inject solutions)? What about other dependency injections? Commented Sep 3, 2015 at 5:48
  • Let the blogs singing over there. The angular way of doing this is your first way, ie, explicitly injecting the dependencies (Moreover: .controller("somename", ['$scope', '$http', function($scope,$http){ ... }]) ). That code is much more clear. Commented Sep 3, 2015 at 5:56
  • @vignesh, are you sure that you need call $apply? can you provide sample plunkr? Commented Sep 3, 2015 at 6:29
  • 2
    you just do it wrong :-) i a bit fix it and $apply not needed, and jQuery also not needed Commented Sep 3, 2015 at 9:21
  • 1
    @vignesh, you just can see about settings for $http.get it same as $.get Commented Sep 3, 2015 at 9:41

3 Answers 3

2

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

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

1 Comment

open the style guide, it recommends using 'this' contrary to scope. He wants to follow that.
1

You're following John Papa's style guide and use the so-called controller-as syntax.

The controller-as syntax allows using instances of functions in views, and therefore it isn't necessary to inject $scope in order to make data available to the view. A simple this.data = mydata is enough.

However, when using special functionality available in $scope, the $scope still has to be injected into the controller function. $apply(), $watch() etc are typically part of that. This article explains exactly that.

About the controllerAs syntax from John Papa's style guide (emphasis mine):

Helps avoid the temptation of using $scope methods inside a controller when it may otherwise be better to avoid them or move the method to a factory, and reference them from the controller. Consider using $scope in a controller only when needed. For example when publishing and subscribing events using $emit, $broadcast, or $on consider moving these uses to a factory and invoke from the controller.

The short answer is therefore no - there is no way of using $scope methods without injecting $scope or an other service which has $scope as a dependency.

2 Comments

Thanks @marapet the $scope is not getting updated if i am not using $scope.$apply after assigning result data to $scope variable, thats why i needed it. Guess i should look for some other way for this instead of using $scope.$apply
What do you mean by '$scope is note getting updated' - in your view? Do you also use the controller-as syntax in your view (in the controller: this.data = ... and in your view: ng-controller="somename as vw" and then {{vw.data}} ?
1

The easiest way to deal with controllers as functions and inject dependencies without breaking anything during the minification process.

(function(){

    'use strict';

    var MainCtrl = function($scope){

       // Do something

    };

    MainCtrl.$inject = [
        '$scope'
    ];

    app.controller('MainCtrl', MainCtrl);

})();

5 Comments

what you want say? :-)
The easiest way to deal with controllers as functions and the way you can inject dependencies without breaking anything during the minification process.
you can add this explain to your answer :-)
I thought was self explanatory because of the context. I've updated the answer. :)

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.