2017-02-15 79 views
0

我正在使用AngularJS在我的控制器中使用onsen和monaca移动应用程序
我需要使用许多变量来进行一些操作,而且我不需要显示它所以根据这个帖子
$scope vs var in AngularJS
我这样
是否有变量之间的关系“var x”和范围

angular.module('myApp').controller('pdfPlanCtrl',['$scope',function($scope) { 
    var newHeight; 
    var newWidth; 
    function drawOldLines(pointBeginX, pointBeginY, pointEndX, pointEndY, color){ 
      ---- some code -------- 
      console.log('drawOldLines scope id '+$scope.$id); 
      console.log('newWidth= '+newWidth); 
    } 
}]); 

在我的控制,我上面说我通过调用函数drawOldLines使用这个变量的一些计算操作定义我的控制器

我从两个位置 调用此功能时,有一个人触摸画布,当有人点击按钮

当有人触摸帆布我得到这样的结果在日志中:

drawOldLines scope id 13 
newWidth = 160 

当我点击我得到的按钮

drawOldLines scope id 22 
newWidth = undefined 

如何在两种情况下newWidth值?或者,也许可以帮助我理解var变量和范围之间的关系?

修订:我添加功能我控制器

+0

我看到一些问题就解决了这里有困惑 - 没有理由说'newHeight'/'newWidth'应该在'drawOldLines'中可用,因为它们被封装在'pdfPlanCtrl'的函数范围内(除非在控制器中定义了'drawOldLines')。基本上,变量只局限于它们定义的JS范围(以及嵌套范围)。但是,如果要将值暴露为Angular'$ scope'的属性,则它们可用于模板/视图中的插值。 –

+0

你是正确的功能是从我的控制器的一部分 –

+0

你可以请提供一些演示在小提琴重现问题。看来你的外码有一些错误。 – Romick

回答

0

里面,当我从

var newHeight; 
var newWidth; 

改变了代码

$rootScope.newHeight; 
$rootScope.newWidth;