Joy Dutta
Joy Dutta

Reputation: 3426

Using closure compiler with 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 ?

Upvotes: 3

Views: 857

Answers (1)

Guillaume86
Guillaume86

Reputation: 14400

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();
}
}]);

Upvotes: 1

Related Questions