Skip to content Skip to sidebar Skip to footer

How Can I Access A Scope Function In A Parent Controller From A Child Controller When Using Typescript?

I have a parent controller AppController for a DIV on my page and a child controller AdminHomeController that's in an area of the page inside that. Here is what I have defined so f

Solution 1:

Here is the complete code:

/// <reference path="angular.d.ts" />

var app = angular.module('app', []);

interface AppControllerScope extends ng.IScope {
    app: AppController;
}

app.controller('appController', AppController);

class AppController {
    static $inject = ['$scope'];
    constructor(public $scope: AppControllerScope) {
        $scope.app = this;
    }
    doTask = () => {
        var x = 99;
    }
}

interface AdminHomeControllerScope extends AppControllerScope {
    home: AdminHomeController;
}

class AdminHomeController {

    public app: AppController;

    static $inject = ['$scope'];
    constructor(public $scope: AdminHomeControllerScope) { // << What should my interface look like?
        $scope.home = this;
        $scope.app.doTask();

        // For easier access if you want it
        this.app = $scope.app;
    }
}

There were a few code formatting (compile errors) in your code sample. I fixed those as well ^


Post a Comment for "How Can I Access A Scope Function In A Parent Controller From A Child Controller When Using Typescript?"