离子控制器功能变量超出范围(Ionic Controller Function Variable Out Of Scope)

基于Ionic的标签模板。 当我单击一个按钮(ng-click)时,它调用函数func()

.controller('AccountCtrl', function($scope) { $scope.number=3; $scope.func=function(){ number=number+123; } });

当我尝试运行func()时。 Chrome会报告以下内容。

ReferenceError: number is not defined

我认为这是因为func()找不到变量号。 有没有办法可以修改函数中的数据?

Based on the tabs template from Ionic. When I click a button(ng-click), it calls the function func()

.controller('AccountCtrl', function($scope) { $scope.number=3; $scope.func=function(){ number=number+123; } });

When I try to run func(). Chrome reports the following.

ReferenceError: number is not defined

I think it is because that func() cannot find the variable number. Is there a way I can modify data in a function?

最满意答案

问题是现在没有一个名为number的变量。 只有将var number放在某处才能声明它,这才会成立。

要修改声明的数值,请确保引用它所属的同一对象。

$scope.func = function() { $scope.number = number + 123; // or $scope.number += 123; };

The problem is that there isn't a variable called number in existence. That would only be true if you had put var number somewhere to declare it.

To modify the number value you have declared, make sure you reference the same object that it's a part of.

$scope.func = function() { $scope.number = number + 123; // or $scope.number += 123; };

更多推荐