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

Using angular services #54

Closed
shturminator opened this issue Nov 27, 2017 · 6 comments
Closed

Using angular services #54

shturminator opened this issue Nov 27, 2017 · 6 comments
Assignees
Labels

Comments

@shturminator
Copy link

Could you please suggest an approach to use sevices/factories inside react2angular component?
Thanks.

@Sawtaytoes
Copy link

Sawtaytoes commented Nov 28, 2017

As a workaround, you should be able to pass them into the viewmodel, something like vm.accountService = accountService and then in the view: <some-react-component accountService="vm.accountService" />.

@bcherny
Copy link
Contributor

bcherny commented Nov 28, 2017

Hi @shturminator! You have 3 options:

Option 1: Simply import your service/factory from your React module. For example, say your factory is (this is from https://docs.angularjs.org/guide/providers):

// factory.js
myApp.factory('apiToken', ['clientId', function apiTokenFactory(clientId) {
  var encrypt = function(data1, data2) {
    return (data1 + ':' + data2).toUpperCase();
  };
  var secret = window.localStorage.getItem('myApp.secret');
  var apiToken = encrypt(clientId, secret);
  return apiToken;
}]);

You can update that to:

// factory.js
var encrypt = function(data1, data2) {
 return (data1 + ':' + data2).toUpperCase();
};
var secret = window.localStorage.getItem('myApp.secret');
export var apiToken = encrypt(clientId, secret);
myApp.factory('apiToken', ['clientId', function apiTokenFactory(clientId) {
 return apiToken;
}]);

Then from your React component, simply import apiToken from factory.js. An added benefit is separating core logic from its Angular bindings.

Option 2 is what @Sawtaytoes suggested. Just DI your service/factory, and pass it to your React component as a regular prop.

Option 3 Once #49 is merged, DI will be supported out of the box.

@bcherny bcherny closed this as completed Nov 28, 2017
@bcherny bcherny self-assigned this Nov 28, 2017
@bcherny
Copy link
Contributor

bcherny commented Dec 11, 2017

With v3.1.0, you can now use the extended DI syntax:

react2angular(Component, props, providersToDI)

See example in the docs.

@shturminator
Copy link
Author

shturminator commented Dec 12, 2017 via email

@Sawtaytoes
Copy link

What if we add propTypes? Do we still need to define DI in this way?

@bcherny
Copy link
Contributor

bcherny commented Dec 15, 2017

@Sawtaytoes Yes, just tell react2angular to infer props from propTypes by passing null as the 2nd argument:

class A {}
A.propTypes = {..}

let B = react2angular(A, null, ['$http', '$log'])

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

No branches or pull requests

3 participants