簡體   English   中英

將綁定應用於控制器后是否發出Angular $ scope事件?

[英]Is there an Angular $scope event emitted after bindings have been applied to a controller?

我在使用bindToController選項的Angular(1.4)應用程序中使用bindToController 這是我的指令定義(用TypeScript編寫):

export class MyDirective implements ng.IDirective {
    public restrict = 'E';
    public templateUrl = 'path/to/my/template.html';
    public controller = 'MyController';
    public controllerAs = 'vm';
    public bindToController = true;
    public scope = {
        myScopeProperty: '@'
    };
}

這按預期方式工作myScopeProperty屬性已正確綁定到控制器。 但是,此綁定過程是在構造對象之后發生的,這意味着在構造對象時,我無法執行任何依賴於此綁定屬性值的邏輯。 例如(再次,在TypeScript中):

export class MyController {

    public myScopeProperty;

    constructor() {

        // this logs "undefined", even if the my-scope-property 
        // attribute on the directive has a value
        console.log('myScopeProperty: ' + this.myScopeProperty); 
    }
}

在Angular將其初始綁定值應用到該對象后,是否有一個$scope事件可以監聽到該控制器對象內?

沒有此類事件,也不應有此類事件,因為單向@綁定意味着myScopeProperty值可能會多次更新。

$scope.$watch('vm.myScopeProperty', () => { ... }));

是觀察綁定更改的推薦方法。

$timeout(() => { ... });

$onInit控制器掛鈎 (可在1.4。中用angular-component $onInit )可用於將代碼推遲到已經插值myScopeProperty的時間(第一次)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM