【问题标题】:Watch parent scope, ng-include in typescript观看父范围,打字稿中的ng-include
【发布时间】:2023-03-05 17:03:01
【问题描述】:

我正在尝试查看子模块中的父属性。

我有这样的html。

<div ng-include="maincontainer.selected"></div>

在我的主容器中,我有这个:

class Maincontainer {
  var:string;
  scope: ng.IScope;
  selected: String;

  static $inject = ['$scope'];    

  constructor($scope: ng.IScope) {
     this.scope = $scope;
     this.var = 'init';
     this.selected = "app/Views/ChildView.html";
  }
}

我想在 ChildViewController 中查看 'var' 父属性。

class ChildView {
     scope: ng.IScope;

     static $inject = ['$scope'];

     constructor($scope: ng.IScope) {
        this.scope = $scope;

        // Here I want to add a watch to the parent property.
     }
}

谁能帮帮我?

【问题讨论】:

  • 你试过$watch$parent.var吗?
  • 问题是 typescript 无法识别 var 的名称并且无法转换
  • 很抱歉,我不懂 TypeScript。但也许this 会有所帮助。
  • 谢谢,但是这个链接并没有解决父作用域的问题,只针对自己作用域。

标签: javascript angularjs angularjs-scope typescript


【解决方案1】:

为了支持 Jahongir 在上述 cmets 中提出的想法...

您可以创建自己的注入接口,该接口将包含所有 $scope 但允许 $parent 为任何类型:

interface IMyControllerScope extends ng.IScope {
  $parent: any;
}

然后将 $scope 声明为您的新接口,它在 $parent 上定义 var

constructor($scope: IMyControllerScope) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多