I am trying to do a simple remoting code base along with auto update to angular table. BUt if I call a remote method as it is async, it gets called late and even though I try to push data in my angular table, it does not render the view correctly.
VF page code (check method addrow)
<apex:page standardStylesheets="false" sidebar="false" showHeader="false" controller="Angular_HelloWorld">
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
</style>
<html ng-app="myApp">
<link rel="stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-controller="personCtrl">
Search table :<br/>
<input type="text" ng-model="criteria" /> <br/><br/>
<table class="table table-striped table-bordered table-hover table-condensed">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Phone</th>
</tr>
</thead>
<tr ng-repeat="x in contacts | filter:criteria">
<td>{{x.id}}</td>
<td>{{x.name}}</td>
<td>{{x.phone}}</td>
</tr>
</table>
<form name="myForm">
First Name :<br/>
<input type="text" ng-model="firstName"/><br/>
Last Name :<br/>
<input type="text" ng-model="lastName" />
<br/>
Phone :<br/>
<input type="text" ng-model="phone" /><br/>
<br/>
<button ng-click="addRow();" >Add</button>
</form>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope, $http) {
$scope.fullName = function() {
return $scope.firstName + ' ' + $scope.lastName;
};
var response =
JSON.parse('{"records": [{"Name": "John Doe","City": "Berlin","Country": "Germany"},{"Name": "Robert","City": "Mumbai","Country": "India"}]}');
$scope.names = response.records;
$scope.contacts = {!contactListJSON};
$scope.reset = function() {
delete $scope.firstName;
delete $scope.lastName;
delete $scope.phone;
}
$scope.addRow = function() {
var contactJS = new Contact();
contactJS.firstName = $scope.firstName;
contactJS.lastName = $scope.lastName;
contactJS.phone = $scope.phone;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.Angular_HelloWorld.insertContact}',
contactJS,
function(result, event) {
console.log('result', result);
$scope.contacts.push({result});
},
{escape:false}
);
$scope.reset();
}
function Contact() {
this.id = null;
this.lastName = null;
}
});
</script>
</body>
</html>
</apex:page>
Controller method (check insertContact method)
public class Angular_HelloWorld {
public static String getContactListJSON() {
List<ContactWrapper> contactWrapper = new List<ContactWrapper>();
for (Contact contactInstance : [SELECT Id, Name, Phone FROM Contact ORDER BY CreatedDate LIMIT 5]) {
contactWrapper.add(
new ContactWrapper(
contactInstance.Name,
contactInstance.Id,
contactInstance.Phone
)
);
}
return JSON.serialize(contactWrapper);
}
@RemoteAction
public static String insertContact(Contact contaceInstance) {
insert contaceInstance;
return JSON.serialize(
new ContactWrapper(
contaceInstance.Name,
contaceInstance.Id,
contaceInstance.phone
)
);
}
public class ContactWrapper {
public String name;
public String id;
public String phone;
public ContactWrapper(String name, String id, String phone) {
this.name = name;
this.id = id;
this.phone = phone;
}
}
}
but when I call remoting on page and then this line -
$scope.contacts.push({result});
It does not work and latest data is not pushed to table.
Any help???
Thanks, Ray
$scope.contacts.push(result);
. But your browser's JavaScript console and debugger are the basic tools you need to learn to do this sort of work.