0

Hello I have this component:

zpc.component("myComponent", {
template: '<button ng-click="$ctrl.find()">CLICK</button>\
                {{$ctrl.results|json}}\ //allways empty
            </div>',
controller: function MyComponent($http) {
    this.text = "";
    this.results = [];
    this.find = function () {
        $http.get("https://swapi.co/api/films/1").then(function (response) {
            this.results = response.data;
            console.log(this.results); //here is data
        });
    }
}
});

After click data is loaded correctly, but will not appear in the view. How to bind data to view from async? Thank you.

1 Answer 1

1

You need to bind the callback function to this:

$http.get("https://swapi.co/api/films/1").then(function (response) {
    this.results = response.data;
    console.log(this.results); //here is data
}.bind(this));

Or avoid using this, and use instead

var vm = this;
vm.find = function () {
    $http.get("https://swapi.co/api/films/1").then(function (response) {
        vm.results = response.data;
        console.log(vm.results); //here is data
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.