Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using closure compiler with AngularJS

Tags:

angularjs

We have been developing a big product with AngularJS and only recently tried to use use closure compiler for syntax checking with the help of jsdoc comments.

I ran into this problem and can't find any help online, including in SO.

Consider a model class written as a service, and using the class name as a type:

ourmodule.factory('OurModel', function() {
    /**
     * @constructor
     */
    var OurModel = function() {};

    return OurModel;
});

ourmodule.controller('Controller1', ['$scope', 'OurModel', function($scope, OurModel) {
    /**
     * @return {OurModel}
     */
    $scope.getNewModel = function () {
         return new OurModel();
    }
}]);

Closure compiler can't recognize 'OurModel'. What am I missing ?

like image 946
Joy Dutta Avatar asked Apr 30 '26 00:04

Joy Dutta


1 Answers

Closure compiler can't guess that the OurModel that you inject to your controller is the same you declared in the factory, angularJS injection pattern make closure compiler useless in that case.

If you declare OurModel in the parent scope, no warning:

var ourmodule = {
  factory: function(a, b){},
  controller: function(a, b){}
};
/**
* @constructor
*/
var OurModel = function(){};

ourmodule.controller('Controller1', ['$scope', function($scope) {
/**
* @return {OurModel}
*/
$scope.getNewModel = function () {
return new OurModel();
}
}]);
like image 52
Guillaume86 Avatar answered May 03 '26 05:05

Guillaume86



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!