简体   繁体   中英

Angular Scope in javascript function with Typescript

I use Typescript with Angular and Breezejs.

class CounterController {
    count: number = 0;
    static $inject = ['$scope'];


    constructor($scope) {
        $scope.vm = this;
    }

    setCount14(): void {  
        this.count = 14; // works
    }

    getQuestions(): void {
        var manager = new breeze.EntityManager('/breeze/dbentities');
        var query = breeze.EntityQuery.from("Corporations").where("Name", "startsWith", "Zen");
        manager.executeQuery(query)
            .then(querySucceeded);

        function querySucceeded(data) {
            this.count= 1; // works not!
        }
    }
}

How can i access the count property in the querySucceeded function correct?

Edit: better: there must be a way to pass a typescript function to executeQuery(query).then ?

Solution: Pass Typescript function as a Javascript function

Then calling the scope.$apply() does apply the bindings.

use (data) => { this.count = 1; } (data) => { this.count = 1; } instead. or your "this" won't have the correct scope. OR as an alternative:

var me = this;
function querySucceeded(data) {
  me.count= 1; // works not!
} 

eg:

getQuestions(): void {
    var manager = new breeze.EntityManager('/breeze/dbentities');
    var query = breeze.EntityQuery.from("Corporations").where("Name", "startsWith", "Zen");
    manager.executeQuery(query)
        .then((data) => { this.count= 1; });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM