fqlx fqlx -4 years ago 152
Javascript Question

How do I add properties to ng.Iscope?

I'm accessing a scope like so:

let element = angular.element(document.getElementsByClassName('active'));
let scope = element.scope();


And my scope object looks like this when I do a

console.log(scope);



$id:2017
$parent:ChildScope
$root:Scope
__private__:Object
index:0
match:Object
__proto__:Object



However when I try to do:

console.log(scope.match);


Typescript gives a syntax error:


(27,34): error TS2339: Property 'match' does not exist on type 'IScope'.


And printing out scope.$id works

console.log(scope.$id);


I know it has something to do $id being defined in the docs so that works. http://definitelytyped.org/docs/angularjs--angular-route/interfaces/ng.iscope.html

How would I print out my member variable
scope.match
without getting a Typescript error? I think I would need to extend ng.IScope?

Answer Source

I was able to fix the problem by exporting an interface and then casting ng.IScope.

Make sure to use ? to make it an optional parameter. That's where I went wrong

export interface IMyScope extends ng.IScope {
  match?: any;
}

And then:

let myScope = <IMyScope>element.scope();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download