vitaminasweb vitaminasweb - 7 months ago 32
Javascript Question

Format AngularJS ng-repeat text obtained from Laravel API

I have a Laravel 5.2 Backend API and a AngularJS Frontend and at some point I perform a laravel validation and return and error if validation fails.

When I iterate trough errors and display them on the frontend I get somthing like this:

["The email has already been taken."]


and I would like to be like this

The email has already been taken.


without the [""] stuff.

My code is:

Angular controller:

if (error.statusText === "Unprocessable Entity") {

$scope.registerErrors = error.data;
}


Angular template:

<div class="alert alert-warning animated pulse" ng-if="registerError && isLoading === false">
<p ng-repeat="(error, errorText) in registerErrors">{{errorText}}</p>
</div>


Laravel controller:

$this->validate($request, [
'firstname' => 'required|max:100|min:3',
'lastname' => 'required|max:100|min:3',
'email' => 'required|email|unique:users|max:255',
]);


Console.log:

enter image description here

Thanks in advance!

Answer

because of errorText contain email array and you shown full email array. if email contain multiple error then can try like:

<p ng-repeat="(error, errorObject) in registerErrors">
  <span ng-repeat=" singleError in errorObject">{{singleError}}</span>
</p>

or for single error can try like:

<p ng-repeat="(error, errorText) in registerErrors">{{errorText[0]}}</p>

or just assign error.data.email instead of error.data

if (error.statusText === "Unprocessable Entity") {
     $scope.registerErrors = error.data.email;
}