Frederik Frederik - 1 year ago 85
AngularJS Question

angularJS v1.5.5 two way binding doesn't work with service

i am trying to retrieve an share data from a resource via an angular service, please see code of the service below:

(function(){var app = angular.module('geoProApp');
app.factory('project', ['$resource','$log',
function ($resource,$log) {
var res = $resource('http://localhost/api/project/:id', { id: '@id' }, {
queryPaged: {
method: 'POST',
isArray: false,
headers: { 'Content-Type': 'application/json' },
url: 'http://localhost/api/project/paged',
//ignore that for now
update: { method: 'PUT' }
var currentData = {
$log.log('initial value');
return {
queryPaged: function (params) {
return res.queryPaged(null, params).$promise.then(function (response) {
$log.log('after data retrival in service');
return response;
data: currentData

In the controller using the service i want to bind to the data, like this:

app.controller('projectList', function ($scope, $state, $stateParams, $log, $http, auth, project, filter, users, bsLoadingOverlayService) {
$scope.totalItems =;
$scope.projects =;

project.queryPaged(helpers.setPagingParams($scope.currentPage, $scope.itemsPerPage, filter)).then(function () {

$log.log('success callback exposed by service');
$log.log('bound values')

However, it seems that bound variables
never get updated, even tho the service retrieves new data, which can be seen here:

output of console

Can someone point me in the right direction what i might missing?

Answer Source

Angular isn't very fond of using primitive datatypes directly. The two way binding doesn't work as expected. Instead of setting the controller scope like this

$scope.totalItems =;
$scope.projects =;

I would instead have an object like this.

$scope.model =;

Read this article that explains why the two way binding doesn't work with primitive types.

A good quote from the article is...

always have a '.' in your ng-models ... Having a '.' in your models will ensure that prototypal inheritance is in play. So, use <input type="text" ng-model="someObj.prop1">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download