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



However when I try to do:


Typescript gives a syntax error:

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

And printing out scope.$id works


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
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