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

Must give users a success or error message

When I eg. sends it right out and text to my json so it must provide true. If it gives true so it must make a success message. But if it does not fit, then it must go and give error.

The problem as it is right now it is that it should return if it is successful or error.

It's just that to only send back whether you have solved the problem right or wrong in relation to time and as well as the text that you have written.

This is how I have built the website in MVC. ASP.net

<input type="hidden" ng-init="GetId='@Model.Id'" ng-model="GetId" />
<div ng-repeat="module in Newslist" style="clear:both; margin:7px 0; min-height:110px; margin:5px 0;">
<div class="col-md-8">

<div ng-show="Succes">
succes
</div>
<div ng-show="Error">
Error
</div>


<div style="clear:both; margin-bottom:10px;" class="col-md-10">
<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>
</div>


Load.js

var app = angular.module('Opgaver', []);
app.controller('OpgaverCheck', function ($scope, $http) {


$scope.CheckValue = function (module) {
//console.log("Hello world " + module.Id + " - " + module.text);

//POST
$scope.$watch("Id", function() {
var url = "/opgaver/kategori/" + $scope.Id + "/" + module.text;

$http.get(url).success( function(response) {
//RETURN TRUE OR FALSE TO .html view. PROBLEM HERE!!

});
})
}
});


Jsonresult here:

[HttpGet]
public JsonResult CheckOpgave(int Id, string Text)
{
var db = Helpers.HelperToTables.DbValue;
var valuetext = Text.ToLower();

var check = db.LektionerOpgaves.FirstOrDefault(i => i.fk_LektionerId == Id && i.CorrectAnswer.Equals(valuetext));
if (check != null)
{
//succes
return Json("SUCCES");
}
else
{
//Error
return Json("ERROR");
}
}

Answer

You should assign the json return to a variable, then use that in your ng-show.

So for example:

$http.get(url).success( function(response) {
            //RETURN TRUE OR FALSE TO .html view. PROBLEM HERE!!
            $scope.jsonMessage = response;

        });

And then in your HTML you will want to do this:

<div ng-show="jsonMessage=='SUCCESS'">
            success
        </div>
        <div ng-show="jsonMessage=='ERROR'">
            Error
        </div>
Comments