Browse Source

make sure all $eval and $interpolate calls are in the right scope

Evan You 10 years ago
parent
commit
a9f4eb1acb
3 changed files with 10 additions and 8 deletions
  1. 4 4
      src/compiler/compile.js
  2. 4 3
      src/directives/model/checkbox.js
  3. 2 1
      src/directives/model/radio.js

+ 4 - 4
src/compiler/compile.js

@@ -335,7 +335,7 @@ function makeTextNodeLinkFn (tokens, frag) {
       if (token.tag) {
         node = childNodes[i]
         if (token.oneTime) {
-          value = vm.$eval(value, scope)
+          value = (scope || vm).$eval(value)
           if (token.html) {
             _.replace(node, templateParser.parse(value, true))
           } else {
@@ -597,11 +597,11 @@ function collectAttrDirective (name, value, options) {
     return {
       def: def,
       _link: allOneTime
-        ? function (vm, el) {
-            el.setAttribute(name, vm.$interpolate(value))
+        ? function (vm, el, host, scope) {
+            el.setAttribute(name, (scope || vm).$interpolate(value))
           }
         : function (vm, el, host, scope) {
-            var exp = textParser.tokensToExp(tokens, vm)
+            var exp = textParser.tokensToExp(tokens, (scope || vm))
             var desc = isClass
               ? dirParser.parse(exp)[0]
               : dirParser.parse(name + ':' + exp)[0]

+ 4 - 3
src/directives/model/checkbox.js

@@ -7,10 +7,11 @@ module.exports = {
     var el = this.el
     var trueExp = this._checkParam('true-exp')
     var falseExp = this._checkParam('false-exp')
+    var scope = this._scope || this.vm
 
     this._matchValue = function (value) {
       if (trueExp !== null) {
-        return _.looseEqual(value, self.vm.$eval(trueExp))
+        return _.looseEqual(value, scope.$eval(trueExp))
       } else {
         return !!value
       }
@@ -19,10 +20,10 @@ module.exports = {
     function getValue () {
       var val = el.checked
       if (val && trueExp !== null) {
-        val = self.vm.$eval(trueExp)
+        val = scope.$eval(trueExp)
       }
       if (!val && falseExp !== null) {
-        val = self.vm.$eval(falseExp)
+        val = scope.$eval(falseExp)
       }
       return val
     }

+ 2 - 1
src/directives/model/radio.js

@@ -7,13 +7,14 @@ module.exports = {
     var el = this.el
     var number = this._checkParam('number') != null
     var expression = this._checkParam('exp')
+    var scope = this._scope || this.vm
 
     this.getValue = function () {
       var val = el.value
       if (number) {
         val = _.toNumber(val)
       } else if (expression !== null) {
-        val = self.vm.$eval(expression)
+        val = scope.$eval(expression)
       }
       return val
     }