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 Controller As Syntax #223

Open
zeeshanjan82 opened this issue Aug 11, 2015 · 7 comments
Open

Using Controller As Syntax #223

zeeshanjan82 opened this issue Aug 11, 2015 · 7 comments

Comments

@zeeshanjan82
Copy link

I am using controller as Syntax and am facing issues while trying to access 'this' variable for the controller inside remote-api-handler function.

@ghiden
Copy link
Owner

ghiden commented Aug 11, 2015

I'm not really catching up with all the latest goodies from Angular lately.
Are you able to create a jsbin or plnkr example?

@dmash
Copy link

dmash commented Nov 12, 2015

Use 'vm.countries' for instance as the 'local-data' attribute. Then as the 'selected object' you can use vm.selectedCountry (name it whatever you like) which you can then access from your controller

@thiphariel
Copy link

Facing the same issue while using ES6 classes. "this" refers to the current scope inside the update function while it should refers to our class.
Any way to avoid this without creepy code?

<angucomplete-alt placeholder="Départ..." pause="250" local-data="vm.places" search-fields="name" title-field="name"  text-searching="Récupération en cours..." no-result="Aucun résultat trouvé"
          input-changed="vm.updateInput"></angucomplete-alt>
class Foo {
   constructor(api) {
       this.api = api;

       this.places = [];
   }

   updateInput(string) {
      console.log(this); // Refers to the controller scope, not the class itself
   }
}

@CovertIII
Copy link

I tried using a closure, but angular gave an infinite digest error.

I changed the code above to this:

    <angucomplete-alt placeholder="Départ..." pause="250" local-data="vm.places" search-fields="name" title-field="name"  text-searching="Récupération en cours..." no-result="Aucun résultat trouvé"
          input-changed="vm.updateInput().update"></angucomplete-alt>
    class Foo {
       constructor(api) {
           this.api = api;

           this.places = [];
       }

       updateInput(string) {
          var self = this;
          return {
              update: function(string){
                  console.log(self); // Refers to class now, but angular gave an infinite digest error
              }
          }

       }
    }

Here's a plunker:

https://plnkr.co/edit/QvXYSejcZE3u4XxqITio

@wendellmva
Copy link

Any progress on this or workaround suggestions. This is only when you inline the controller in html ng-controller="SomeController as vm" the digest errors occur. The same controller from a route with controllerAs works but SomeController as vm gives the same digest errors as inline. Any help?

@ghiden
Copy link
Owner

ghiden commented Mar 26, 2016

@wendellm Can you create a plnkr example?

@CovertIII
Copy link

So finally, I think I got figured this out. Using @thiphariel's example

    <angucomplete-alt placeholder="Départ..." pause="250" local-data="vm.places" search-fields="name" title-field="name"  text-searching="Récupération en cours..." no-result="Aucun résultat trouvé"
          input-changed="vm.updateInput().update"></angucomplete-alt>
    class Foo {
       constructor(api) {
           this.api = api;

           this.places = [];
           var self = this;
           this.updateInput = function(string) {
             console.log(self); // Refers to the class now
           }
        }

}

Updated plunkr: https://plnkr.co/edit/wjWPkoKtILEeRVHjtBJY?p=preview

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

No branches or pull requests

6 participants