Service Portal: ng-repeat not working

Vinay Krishna1
Kilo Guru

Hi all,

 

I'm trying to create a very basic widget where the input field takes value and updates the DATA object every time the function is clicked. And then I display it using ng-repeat int he HTML view. But I ran across into an issue where ng-repeat is not showing the list of all items I entered, instead only showing the last one. Please look into the issue. 

 

HTML: 

<ul><li ng-repeat='x in c.data.items'>{{x}}</li></ul>

<input type='text' ng-model='c.data.message'/>
<button ng-click='c.additem()'>Add</button>

Client script:

function($scope) {
var c = this;

c.additem=function(){

c.server.update().then(function(response){
c.data.message='';
});
}}

Server script: 

(function() {
data.items=[];
if(input)
{
data.items.push(input.message);
}

})();

 

I've tried uisng c.server.get() as well like the below one. It's still not working. 

c.server.get({additem:c.data.message}).then(function(response){

c.data.message='';

c.data.items=response.data.items;});

Server:

if(input.additem)

data.items.push(input.additem);

1 ACCEPTED SOLUTION

Vinay Krishna1
Kilo Guru

I've found a way to make it work. You just need to add this line in the server script. What it does is it stores the previous value of data.items through input.items. 

 

data.items=[];

if(input)

{

data.items=input.items;
data.items.push(input.message);
}

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hello Vinay,

Well, looking at your code, ng-repeat goes good with table tag.

Please look to the similar code that displays all the items,

HTML :

<table class="table table-bordered">
<thead>
<tr>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr >

<td><input type='text' ng-model="c.data.message"></td>

</tr>
<tr ng-repeat="m in c.data.dataFromFields track by $index">
<td>{{m.message}}</td>

</tr>
</tbody>
</table>
<button type="button" ng-click="c.add()" class="btn btn-warning">Add</button>

 

CLIENT SCRIPT: 

function($scope) {
/* widget controller */
var c = this;

c.add = function(){
c.data.action = "add";
c.server.update().then(function(){

c.data.action=undefined;

//Clear the Boxes.
c.data.message = "";

});

}
}

 

SERVER SCRIPT:

 

(function() {

data.dataFromFields = [];
if(input){
data.dataFromFields = input.dataFromFields;

}
if(input.action=="add"){

var dataArray={};
dataArray.message = input.message;
data.dataFromFields.push(dataArray)
data.dataFromFields.forEach(function(item){
gs.addInfoMessage(item)
})

}

})();

 

I'll try to simply the code more if you want.

Please mark helpful if that served your requirement.

 

Regards,

Kush

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vinay,

can you check below link if it helps

https://community.servicenow.com/community?id=community_article&sys_id=4c639526db5d7b4ce0e80b55ca961...

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Vinay Krishna1
Kilo Guru

I've found a way to make it work. You just need to add this line in the server script. What it does is it stores the previous value of data.items through input.items. 

 

data.items=[];

if(input)

{

data.items=input.items;
data.items.push(input.message);
}