我如何使用打字稿,当访问一个范围的功能在父控制器从子控制器? [英] How can I access a scope function in a parent controller from a child controller when using Typescript?
问题描述
我有我的网页上DIV和子控制器 AdminHomeController
这是在一个地区父控制器 AppController的
里面的那个页面。以下是我迄今为止所定义。
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 far.
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;
}
}
class AdminHomeController {
static $inject = ['$scope']
constructor( public $scope: ?? ) { // << What should my interface look like?
$scope.home = this;
app.doTask();
}
app = $scope.app; // How can I set up a parameter of this controller
// that can be used on my web page or inside this
// controller?
}
有人能告诉我如何从访问父控制器功能doTask()子控制器内部,也是我的网页上?我认为不知何故,我要在这里设置接口:
Can someone tell me how I can access the parent controller function doTask() from inside the child controller and also on my page? I assume that somehow I have to set an interface here:
constructor( public $scope: ?? ) {
但我不知道它应该是什么样子。
But I am not sure what it should look like.
推荐答案
下面是完整的code:
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;
}
}
有几个code格式(编译错误)的code样本。我固定的,以及^
There were a few code formatting (compile errors) in your code sample. I fixed those as well ^
这篇关于我如何使用打字稿,当访问一个范围的功能在父控制器从子控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!