0

Here I have my index controller which is in the index component which includes several functions that I call later in another controller:

userParticipate() {
    this.ApiPartners.feliwayParticipate().then(res => {
      this.$state.go('feliwayMain.success')
    }).catch((err) => {
      this.errorMessage = err.data.error
      console.log('err', this.errorMessage)
    })  

    }

And now here is another controller that is in another component to which I pass the function (thanks to bindings):

validateChoosePet(optin, response) {
    if (this.selectedPet) {
      if (optin == true) {
        this.ParticipateOptin()
      } else {
        this.Participate()
        console.log('erruser', this.errorMessage)
      }
     }
    }

The function ParticipateOptin() is the same as userParticipate() it just allows users to have newsletter.

Everything works fine, but I would like to display errors that happen in userParticpate() at validateChoosePet (). I'm getting errors in the console.log just impossible to display them

I hope I have succeeded in making myself understood, sorry for my English.

mathiasF
  • 267
  • 1
  • 4
  • 14
  • What do you mean by display? Do you already have the error in another controller and you just can't display it, or do need to share the error between controllers in the first place? – Aleksey Solovey Oct 23 '17 at 10:08
  • sorry, I did not express myself well. I want to display the error when I click on a button, when I click on the button the error appears in the console but not on the web page.Here is the template: `

    {{ ::$root.t.photoshoot.choose_pet_none }}

    `
    – mathiasF Oct 23 '17 at 10:24
  • @AlekseySolovey and yes i need to share the error between controllers in the first place – mathiasF Oct 23 '17 at 10:29
  • Think of having global variable to share. One solution: have a parent controller which holds the variable that both child controllers can access. Second is to use `$rootScope`, a global scope. Since it's watched every time, filling it in with variables and making it heavy is a bad idea (makes your app slow). So instead you can use a service/factory that stores your variables and you can update/access them on some changes in any controller. – Aleksey Solovey Oct 23 '17 at 10:33
  • To check for changes you can use angularjs constants: `app.constant('YOUR_EVENTS', {somethingChanged: 'variable-SOMETHING-changed'})`. Then you can broadcast a change with `$rootScope.$broadcast(YOUR_EVENTS.somethingChanged)` and detect it somewhere else with `$rootScope.$on(YOUR_EVENTS.somethingChanged, youFunctionCallback)`. (Don't forget to inject YOUR_EVENTS constant in your controllers) – Aleksey Solovey Oct 23 '17 at 10:36
  • ok I understand, thank you very much I will test all that. I'll start by adding the variable in the service – mathiasF Oct 23 '17 at 12:01
  • More [related topic](https://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers) for your tests – Aleksey Solovey Oct 23 '17 at 12:05
  • @AlekseySolovey Super thank you I look at it if it does not work. But I think you put me on the right path :) . – mathiasF Oct 23 '17 at 12:13

0 Answers0