【问题标题】:How to access parent scope in Angular with typescript如何使用打字稿访问Angular中的父范围
【发布时间】:2015-08-04 06:36:06
【问题描述】:

如何在 typescript-class 中访问父 Controller 的范围?

这是我的代码:

 export class EntityOverviewCtrl extends AbstractCtrl {

    public static $inject = ["$state", "$http", "CurrentSession"];

    public EntityName: string;
    public Records: any[] = [];        

    constructor($state, $http, $parent, protected currentSession) {
        super($state, $http);

        this.init();
    }
}

我正在使用 UI-Router,但此处没有可用的 $scope.$parent-Variable...

【问题讨论】:

  • 你可以在你的控制器中注入 $scope。你为什么不这样做?
  • 我试过了,但没用。$scope 总是未定义的。我将“$scope”添加到注入变量中,并将 $scope: ng.IScope 添加到构造函数中,但没有运气
  • 您可以发布该代码吗?我怀疑你做错了什么。否则它应该工作
  • 抱歉,另一个类中出现错误,VS 没有重新创建我的 Javscript 文件。我必须在继承 EntitiyOverviewCtrl 的其他类中包含 $scope。感谢您的帮助

标签: angularjs typescript


【解决方案1】:

如果您使用“as”语法:

static $inject = ["$scope"];
constructor(private $scope: ng.IScope) {
    this.$scope.$parent.vm.changedProperty = yourValue;
}

【讨论】:

  • 确实 vm 对我来说就是这种情况
【解决方案2】:

没有 $scope.$parent

$parent 应该指向父作用域。错误在别处。

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多