Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot annotate properly when the controller is inside an object #187

Closed
ericcky opened this issue Aug 6, 2015 · 7 comments
Closed

cannot annotate properly when the controller is inside an object #187

ericcky opened this issue Aug 6, 2015 · 7 comments

Comments

@ericcky
Copy link

ericcky commented Aug 6, 2015

var myApp = angular.module('myApp', []);
var obj = {};
obj.ctrl = function ($scope) {};
myApp.controller('myCtrl', obj.ctrl);

#183

I found that if controller is in a object, ng-annotate will ignore it. Is it possible to support that structure?
thanks.

@olov
Copy link
Owner

olov commented Aug 6, 2015

Will that code even work in Angular? If so, can you please link to documentation for the .ctrl property?

@ericcky
Copy link
Author

ericcky commented Aug 6, 2015

yes, it works in angular.
http://plnkr.co/edit/D1YwtPZXxOekK7xsnWsV?p=preview

@olov
Copy link
Owner

olov commented Aug 6, 2015

Forgive me I didn't look at your code closely enough and thought that you were sending in obj instead of obj.ctrl as a parameter. Ok, now I get your question. No, that won't be supported but feel free to do obj.ctrl = function ($scope) { "ngInject"; ... }; and things will work fine.

@olov olov closed this as completed Aug 6, 2015
@ericcky
Copy link
Author

ericcky commented Aug 7, 2015

Thanks for your reply, this is work for me, but adding "ngInject"; one by one is really annoying.

@olov
Copy link
Owner

olov commented Aug 7, 2015

hopefully not as annoying as manually creating an inject-array and keeping it consistent with the parameters.

@DSchau
Copy link

DSchau commented Oct 15, 2015

Just a note that this may become more of an issue as people begin to use something like Webpack to bundle assets together.

Consider the following code:

'use strict';
function LoadingInterceptorConfig($httpProvider) {
  $httpProvider.interceptors.push('LoadingInterceptor');
}

export default LoadingInterceptorConfig;

then the import, itself:

'use strict';
import LoadingInterceptorConfig from './sample-loading-interceptor.config';

export default angular.module('sample.loadingInterceptor', [])
.config(LoadingInterceptorConfig);

which creates (before ng-annotate runs) the following structure, which is essentially doing something similar to the problem described, e.g. accessing an object property to get the function reference.

var _sampleLoadingInterceptorConfig = __webpack_require__(2);

var _sampleLoadingInterceptorConfig2 = _interopRequireDefault(_sampleLoadingInterceptorConfig);

'use strict';
exports['default'] = angular.module('sample.loadingInterceptor', [])
.config(_sampleLoadingInterceptorConfig2['default'])

module.exports = exports['default'];

However, as @olov noted, adding the following does work, it'd just be ideal if somehow ng-annotate was able to work with a module bundler/object functions.

'use strict';
function LoadingInterceptorConfig($httpProvider) {
  'ngInject';
  $httpProvider.interceptors.push('LoadingInterceptor');
}

export default LoadingInterceptorConfig;

@olov
Copy link
Owner

olov commented Oct 19, 2015

@DSchau it's just hard to do that with static analysis and I don't believe it will happen. "ngInject"; is not that bad! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants