snorlax snorlax - 4 months ago 11
jQuery Question

It's not possible to get the value from a html file using a controller Angularjs

I have a page html with an input text field and this input text has controlled by a checkbox. When the checkbox is false it appears and when the checkbox is true the input text show itself.
This is work very well, but when I try to get the value that the user put in this field, the controllerJS is not able to get it.

I have used this code:

<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>


I have used this code to pass the variables

<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>


This is my controller:

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

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


My goal is to print, but I'm not able to do it.

I thank you in advance for your help.

Answer

If you look at your dev console in your web browser, it will show you that it can't read "subjectReceive" of undefined.

So, you need to initialize the gmailinput variable in the controller because you are using it as an object.

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

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

Depending on what you are trying to accomplish, you can also do what @BrianRay suggested. And remove the user from the parameters, and use the $scope.gmailinput object instead. Because you have the gmailinput on the scope, you don't need to pass it in.

You still need to initialize the object nonetheless.