0

I'm having a problem getting the values from multiple input fields, into a single string. The way I am currently trying to do it is to make each input field an index in the array. I made an array, guess, then I'm breaking down a string into individual characters, and outputting one input field per character, each input field has the ng-model of guess[i].

    .controller('challengeCtrl', function($scope, challenge, user, $http, $ionicModal, $sce) {

$scope.guess = [];

$scope.giveUnderscores = function() {
        var answer = challenge.selectedChallenge.answer;
        var underscore = '';
        for (var i = 0; i < answer.length; i++) {
            if(answer[i+1] == ' '){
              underscore += '<input type="text" style="margin-right:1em;" maxlength="1" ng-model="guess[' + i + ']" class="underscore">';  
            } else if (answer[i] != ' ') {
                underscore += '<input type="text" maxlength="1" ng-model="guess[' + i + ']" class="underscore">';
            } else {
                $scope.user = user;    
                underscore += '<span></span>';
            }
        }
        return underscore;
    }

The string that is being returned is being bound to the HTML.

<div ng-bind-html="fake">
        <!-- String goes here -->
    </div>

And then I want to hit a submit button that will call a function to test the array.

<button ng-click="checkAnswer(guess)">
    SUBMIT GUESS
</button>

But right now the ng-model comes back undeclared every time and I can't seem to get it the value from the input fields into the array.

Thanks!

Lehi
  • 15
  • 5
  • Not sure if this helps or not, but https://docs.angularjs.org/api/ng/directive/ngBindHtml . Read the first paragraph on `ngBindHtml` regarding needing a `$sanitize` injection to use it. – mariocatch Apr 26 '16 at 01:22
  • Thanks for the tip, but I looked into it and I think I'm still good. While I'm not Sanitizing I am doing it the other way they mention in the 2nd paragraph. $scope.output = $sce.trustAsHtml($scope.giveUnderscores()); – Lehi Apr 26 '16 at 11:01
  • So I noticed if I hard code the HTML in instead of making it with a for loop and "trusting" it as HTML, that my array passes in through ng-model no problem. I tried the $sanitize route rather than the $sce.trustAsHtml way but didn't have any luck that way either. – Lehi Apr 27 '16 at 05:15

0 Answers0