角度js中的范围访问

时间:2016-04-28 07:41:06

标签: javascript angularjs ionic-framework

我无法使用angularjs和ionic访问变量。我有一个页面,我列出了一些项目:

<ion-item ng-repeat="item in items" 
              item="item"
              href=""
              style="max-height:70px; padding: 0px 0px 0px 0px;"
              >             

             <div class="containerText" style="position:absolute; float:left; margin-left:60px">
                <div class="" style="font-size:11px">               
                    {{item.datea}}
                </div>
                <div class="" style="margin-top:4px; font-weight:bold; text-align:right">
                    {{item.value}}                  
                </div>
            </div>
(...)

当我按下&#34;下载&#34;我想加载item.value。按钮,每个项目行都在删除图标旁边:

screenshot of the app

我将所有数字转换为输入字段中的字符串,以便更容易控制更改(例如添加小数点或删除该字符串的最后一个值)。

我的controller.js具有以下功能:

$scope.searchText = "0";

(...)

/****************** KEYBOARD FUNCTIONS ************************/    
//Adds a number in the field text above the keyboard
$scope.addNumber = function(num) {

 //limiting the amount of numbers in the display 
 if (this.searchText.length < 20)
     if (this.searchText == "0")
         if (num != '.')
             this.searchText=num;
         else
             this.searchText = (this.searchText) + num;
     else
         if (num!='.')
             this.searchText = (this.searchText) + num;
         else if (this.searchText.indexOf('.') == -1)
             this.searchText = (this.searchText) + num;             
}

//Used to delete the number in the screen and set it to 0  
//AKA RESET
$scope.deleteNumbers = function(){ 
  this.searchText= "0"; 
  console.log("ERASING!");
}

//Used to delete the last entered digit/symbol
$scope.deleteLast = function(){
     this.searchText = this.searchText.slice(0, -1);
     if (this.searchText.length==0)
     {
        this.searchText = "0";
     }  
}  

//used to load the values of an element of the list to the display
$scope.loadItem = function(valueToAdd){

    console.log("Load values ");

    $scope.deleteNumbers();
    $tempVal = valueToAdd.toString();   
    $scope.addNumber($tempVal);

    console.log("value in this.searchText " + this.searchText);
    console.log("value in scope.searchText " + $scope.searchText);
    console.log(isNaN($tempVal));
}

它可以很好地加载第一次执行(只有在我之前没有按下编码键盘的情况下),但是在触摸我创建的键盘后,&#34;加载编号&#34;功能停止工作。

当我加载页面并按下其中一个下载按钮时,我的console.log看起来像这样:

controllers.js:185 Load values 
controllers.js:77 ERASING!
controllers.js:212 value in this.searchText 14458.553
controllers.js:213 value in scope.searchText 14458.553
controllers.js:214 false

正如您所看到的,它表示它是一个已加载到字段中的数值,但它让我添加其他数字或删除输入字符串的字符而没有问题,但如果我想要加载另一个值(甚至是之前加载的相同值)似乎在获得此输出时范围变得混乱。

controllers.js:185 Load values 
controllers.js:77 ERASING!
controllers.js:212 value in this.searchText 14458.55
controllers.js:213 value in scope.searchText 15429.477
controllers.js:214 false

我尝试过同时修改this.searchText和scope.searchText但没有结果。我该怎么做才能解决这个问题?

0 个答案:

没有答案