打字稿:属性在$ rootScope(Angularjs)上不存在 [英] Typescript: Property doesn't exist on the $rootScope (Angularjs)

查看:116
本文介绍了打字稿:属性在$ rootScope(Angularjs)上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Typescript中将值添加到rootscope时出错.

Getting an error when adding a value to the rootscope in Typescript.

class TestClass{
   this.rootScope: ng.IRootScopeService;
   constructor($rootScope){
       this.rootScope = $rootScope;
    }

    addValueToRoot=()=>{
       this.rootScope.val1 = "something";    //Error: Property doesn't exist on the IRootScopeService
    }
}

推荐答案

这是因为(如编译器所说的)val1ng.IRootScopeService上不存在.您需要扩展它以使其适应您的需求,例如

That is because (as the compiler says) val1 does not exist on ng.IRootScopeService. You need to extend it to adapt it to your needs, e.g.

interface MyRootScope extends ng.IRootScopeService {
  val1: string
}

然后您可以在课堂上使用此界面:

Then you can just use this interface in your class:

class TestClass {
  this.rootScope: MyRootScope;
  ...
}

这篇关于打字稿:属性在$ rootScope(Angularjs)上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆