Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
fqlx Avatar asked Sep 19 '25 04:09

fqlx


1 Answers

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();
like image 105
fqlx Avatar answered Sep 22 '25 01:09

fqlx