jadjoubran / laravel5-angular-material-starter

Get started with Laravel 5.3 and AngularJS (material)
https://laravel-angular.readme.io/
MIT License
1.66k stars 400 forks source link

unable to write this in response #427

Closed kingvish closed 7 years ago

kingvish commented 7 years ago

I want populate some data on init and getting this this is undefined error in console. however, i am getting correct value in alert. I am new to this, please suggest if anything is wrong..

class InstCallVisitController{
    constructor(API){
        'ngInject';
        this.API = API;
      //  this.$scope = $scope;
        //this.ToastService = ToastService;

        this.myWelcome = "";
        this.visitor_collection =  [];
        this.institute_collection = [];
        this.category_collection = [];
        this.inquiry_converted_collection = [];

    }

    $onInit(){
        alert('calling init method!');

        this.API.all('call-visit-search-init-data').get([]).then(function (response) {

            alert(response.visitor_collection[0].visitor);
            this.myWelcome  = response.visitor_collection[0].visitor;

        });

    }

    submit(){
        var data = {
            visitor: this.visitor,
            institute: this.institute,
            category: this.category,
            inquiry_converted: this.inquiry_converted,
            visit_date_from: this.visit_date_from,
            visit_date_to: this.visit_date_to
        }

        this.API.all('/inst_call_visit/search').post(data).then(function (response) {
            this.myWelcome = response.data;
        });

    }

}

export const InstCallVisitComponent = {
    templateUrl: './views/app/components/inst_call_visit/inst_call_visit.component.html',
    controller: InstCallVisitController,
    controllerAs: 'icv',
    bindings: {}
}
puahyan commented 7 years ago

you mean this.myWelcome ?? if yes, in your view ('./views/app/components/inst_call_visit/inst_call_visit.component.html), where is {{ icv.myWelcome}} ? this mean your "icv" controllername

kingvish commented 7 years ago

i wrote this in view for testing purpose:

<div class="form-group">
         <label>Visited. By</label>     {{ icv.myWelcome }}
        <select ng-model="icv.visitor" class="form-control">
                <option ng-repeat="x in icv.visitor_collection" value="{{x.id}}">{{x.visitor}}</option>
       </select>
</div>
jadjoubran commented 7 years ago

Hi @kingvish You need to use arrow functions in order to have the same value for this inside the function as well Here's a simular issue

You can also check the 10 minute intro to ES2015 tutorial