2016-08-02 60 views
0

我有一個帶有輸入文本字段的頁面html,並且此輸入文本由複選框控制。當複選框爲false時,它會出現,當複選框爲真時,輸入文本將顯示。 這是非常好的工作,但是當我嘗試獲取用戶在此字段中輸入的值時,controllerJS無法獲取它。使用控制器無法從html文件中獲取值Angularjs

我已經使用這個代碼:

<div class="form-group"> 
    <label> 
     <input 
     type="checkbox" 
     ng-model="checkedSubject" 
    > 
<img src="../../innerPages/gmailChannel/email-text-search.png" height="30" width="30"/> Email content query: 
</label><br/> 
    <textarea 
     class="form-control animate-if" 
     ng-if="checkedSubject" 
     rows="5" id="subjectReceive" placeholder="Insert here some text to filter the emails" 
     ng-model="gmailinput.subjectReceive" name="subjectReceive" ng-disabled="!checkedSubject" 
     maxlength="100%" ng-trim="false" 
    ></textarea> 
    <span>{{500 - gmailinput.subjectReceive.length}} left </span></div> 

我已經使用這個代碼傳遞變量

<div> 
    <a type="submit" class="btn btn-danger pull-left" href="#allTriggers" >Cancel</a> 
    <a href="#createRecipeAction" ng-disabled="!checkedSubject" type="submit" 
     class="btn btn-primary pull-right btn_next3" ng-click="triggerGmail(gmailinput)" >Next</a> 
</div> 

這是我的控制器:

ftttApp.controller('GmailTriggerController', ['$scope', '$rootScope', '$routeParams', '$http', '$location', 
function ($scope, $rootscope, $routeParams, $http, $resource, $location) { 
    $scope.triggerGmail = function(user) 
    { 

     alert(user.subjectReceive); 
     }; 
}]); 

我的目標是打印,但我無法做到。

我事先感謝您的幫助。

+0

你的ng模型是'gmailinput.subjectReceive'。因此,你的提醒應該是alert($ scope.gmailinput.subjectReceive);'。 –

+0

這是行不通的。我改變了我的警報,我的瀏覽器的控制檯說:「錯誤:undefined不是一個對象(評估'$ scope.gmailinput.subjectreceive')」 – snorlax

回答

0

如果您在Web瀏覽器中查看開發控制檯,它會告訴您它不能讀取未定義的「subjectReceive」。

因此,您需要初始化控制器中的gmailinput變量,因爲您將它用作對象。

ftttApp.controller('GmailTriggerController', ['$scope', '$rootScope', '$routeParams', '$http', '$location', 
function ($scope, $rootscope, $routeParams, $http, $resource, $location) { 
$scope.gmailinput = []; 
    $scope.triggerGmail = function(user) 
    { 

     alert(user.subjectReceive); 
     }; 
}]); 

根據你想要完成的工作,你也可以做@BrianRay建議的。並從參數中刪除用戶,並改爲使用$ scope.gmailinput對象。因爲你在示波器上有gmailinput,所以你不需要通過它。

你仍然需要初始化對象。

相關問題