-3

Let's say one example <p>{{myVariable + 1}}</p>

In my understanding, I could only come up with parse it to a Function, and provide it with a context.

However, this statement would thus become <p>{{this.myVariable + 1}}</p>.

How did angular get rid of this ?

Qiaosen Huang
  • 1,093
  • 1
  • 10
  • 25
  • Eval it and give it the scope context? – Shomz Jul 06 '15 at 01:41
  • `this` is the current controller. Without `this` you're working with scopes. http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers – Arnold Daniels Jul 06 '15 at 01:42
  • @Jasny From what I understand you can only use `this` on the controller side, in view side you have to use the controller alias in `as` – Icycool Jul 06 '15 at 02:54

2 Answers2

2

Angular didn't get rid of this. In angular, you use scopes to communicate values between controllers and views. this is the function execution context, which means it's the object the current function or property is being called on.

But AngularJS isn't written to use this in databindings, it's written to specifically parse those {{}} databindings and look for properties on the corresponding controller's scope object.

So Angular sees the following:

<div ng-controller="mainCtrl">
    <p>{{myVariable}}</p>
</div>

And knows to go to the mainCtrl controller and look in the $scope object to find the myVariable variable. this never enters into it.

whatoncewaslost
  • 2,216
  • 2
  • 17
  • 25
0

I figured out a way to do this.

var env = '';
for (var key in context) {
    env = "var " + key + " = this." + key + ";\n";
}
var fn = new Function(env + 'return ' + script);
fn.call(context);
Qiaosen Huang
  • 1,093
  • 1
  • 10
  • 25