J. Petersen J. Petersen - 3 months ago 7
AngularJS Question

Does not send my value from button and text over to my js file

This is how I try to send my text above and also send my module ID over to my js file. I would have liked me to the enter the text you wrote and the id you got clicked.

The problem is that it comes back and gives me this:

Hello world undefined - undefined

It would have looked like this:

Hello world 3 - Tetetew twt ewtewt

What I'm trying to is that I need to send LektionerId from the button over to my js file and at the same time I send text over to my js file.

js - file here:

$scope.CheckValue = function () {
console.log("Hello world " + $scope.LektionerId + " - " + $scope.Text);


//$scope.$watch("OpgaveId", function () {
// var url = "/opgaver/CheckOpgave/" + $scope.LektionerId ;

// $http.get(url).success(function (response) {
// $scope.Newslist = response;
// });
//});
}


index.html

<div ng-repeat="module in Newslist">
<div class="col-md-8">
<input type="text"
ng-model="Text"
class="form-control"
placeholder="Write your answer here"
name="Text" />

<button ng-click="CheckValue()"
ng-init="LektionerId='{{module.Id}}'"
class="btn btn-primary pull-right"
style="margin:6px 0;">
Check your answer
</button>
</div>
</div>


or should I try to put it all in a form?

Answer

To get each module value you should pass it like this: CheckValue(module) and I would remove entrirely the ng-init (https://code.angularjs.org/1.4.12/docs/api/ng/directive/ngInit)

Atm all input fields you create within the loop are bound to the same model (ng-model="Text"). In case you want different values, then you will need to provide different models.

<div ng-repeat="module in Newslist">
        <div class="col-md-8">
                <input type="text" 
                       ng-model="module.text" 
                       class="form-control" 
                       placeholder="Write your answer here" 
                       name="Text" />

                <button ng-click="CheckValue(module)" 
                        class="btn btn-primary pull-right"
                        style="margin:6px 0;">
                    Check your answer
                </button>
        </div>
    </div>

$scope.CheckValue = function (module) {
    console.log("Hello world " + module.id + " - " + module.text);
}
Comments