Lehi
Lehi

Reputation: 27

Passing array with ng-model

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!

Upvotes: 0

Views: 908

Answers (0)

Related Questions