2

I want to show error array in span using repeat. Can you please tell me what our best way to do it.

I create span and attached to existing structure. But I want to try this way. I am new to angular.

var errorArray = [];
                 errorArray.push("Test Showing multiple errors1");
                 errorArray.push("Test Showing multiple errors2");
                 //This will show all the colounms 
                 for (let key in error.data) {
                     if (error.data.hasOwnProperty(key)) {
                         console.log(error.data[key]);
                         errorArray.push(error.data[key]);
                     }
                 }
                 vm.error = errorArray;
             });

HTML side Code

<span class="help-block" ng-model="vm.error" ng-repeat="data in vm.error">{{vm.error}}</span>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
nikunjM
  • 560
  • 1
  • 7
  • 21
  • This has already been answered in this SO post: https://stackoverflow.com/questions/23528478/angularjs-ng-model-fails-on-span – rrd Dec 11 '17 at 17:31
  • @rrd I saw that question but it doesn't say how to use ng-repeat with span. It's different issue. – nikunjM Dec 11 '17 at 17:33
  • 5
    Ahhh perhaps then, instead of showing {{vm.error}} you should show {{data}} – rrd Dec 11 '17 at 17:34
  • @rrd that worked, thank you – nikunjM Dec 11 '17 at 17:35
  • 2
    `ng-model` is meant for form control elements – charlietfl Dec 11 '17 at 18:25
  • To see how to make a `` element work with the `ng-model` directive, see [AngularJS `ngModelController` API Reference - Custom Control Example](https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#custom-control-example). – georgeawg Dec 11 '17 at 19:06

2 Answers2

0

in ng-repeat you replace data with any element of vm.error array and ng-model couldn't be in span tag. try following:

<span class="help-block"  ng-repeat="data in vm.error">{{data}}</span>
Aref Zamani
  • 2,023
  • 2
  • 20
  • 40
0

cause you are iterating on the data object. so only thing available to your expressions would be data and not vm.error. Its like the value of i in for loops.

<span class="help-block"  ng-repeat="data in vm.error">{{data}}</span>
ProblemSolver
  • 636
  • 1
  • 8
  • 16