2016-01-31 39 views
0

我有簡單的離子模板是這樣的:對同一控制器中另一個方法的離子調用不確定?

<ion-item class="range range-calm">Shift {{shiftval}}: 
     <input ng-change="shiftChange()" type="range" ng-model="shiftval" min="1" max="25"> 
    </ion-item> 
     <span class="input-label">In</span> 
     <input type="text" ng-model="decripted" id="dec" ng-change="change()" /> 
    </label> 
     <span class="input-label">Out</span> 
     <input type="text" ng-model="encripted" /> 
    </label> 

與控制器的邏輯是這樣的:

.controller('mylog', function($scope) { 
    var shiftval = (typeof shiftval === 'undefined') ? 13 : shiftval; 
    $scope.change = function(){ 
     this.decripted = this.decripted.toUpperCase(); 
     this.encripted = cezarCipher(this.decripted, shiftval); 
    }; 
    $scope.shiftChange = function(){ 
     shiftval = this.shiftval; 
     $scope.change(); 
    }; 
}) 

的最後一行,我嘗試調用$scope.change();簡單地觸發事件,並再次重新渲染文本我得到這樣的錯誤:

Cannot read property 'toUpperCase' of undefined就行了.toUpperCase()在該fn中。

如果有人有想法如何克服這一點,我會非常感謝!

+0

你需要使用$ scope.decripted。這個.decripted是undefined –

+0

試過了,但是也失敗 –

回答

1

試着給$scope.decripted一個初始值,它可能是第一次在控制器中沒有設置$scope.change函數被觸發。

由於decripted未定義,因此試圖調用函數toUpperCase()會觸發錯誤,因爲它不是字符串。

+0

謝謝,但是我console.log'ed它,它在im在change()fn中有值,但是當我從shiftChange()調用它的時候,可以你發佈的代碼放在哪裏? –

0

你應該通過參數作爲ng-modelfunction

<input type="text" ng-model="decripted" id="dec" ng-change="change(decripted)" /> 

和你controller這樣

$scope.change = function(decripted){ 
    var decripted = decripted.toUpperCase(); 
    encripted = cezarCipher(decripted, shiftval); 
}; 
+0

沒有幫助@Ved,我得到同樣的錯誤 –

相關問題