2016-08-02 58 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,所以你不需要通过它。

你仍然需要初始化对象。

相关问题