Im have data stored in a scope.
I want to update the scope with new values.
So first my angular code looks like this
$scope.objects = [
{id: 1, name: "ma route angulaire1", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"},
{id: 2, name: "ma route angulaire2", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"},
{id: 3, name: "ma route angulaire3", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"}
];
and it is displayed on the page using ng-repeat
<div ng-controller="objectCtrl" >
<div id="object_container" ng-repeat="object in objects track by $index">
<div>{{object.name}}</div>
</div>
So far so good but then I update the scope using apply with a new array that comes from my ajax script (not inside the angular app or controller)
var scope = angular.element($("#object_container")).scope();
scope.$apply(function(){
console.log(scope.objects); // before update
scope.objects = data[1];
console.log(scope.objects); // after update
});
So in the console I can see the new values asign to the scope but on the page itself it is not getting updated.
Edit
My code; Angular
'use strict';
var angular_app = angular.module('angular_app', []);
angular_app.controller('objectCtrl', ['$scope', function ($scope) {
$scope.objects = [
{id: 1, name: "ma route angulaire1", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"},
{id: 2, name: "ma route angulaire2", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"},
{id: 3, name: "ma route angulaire3", cote: "10.5", style: "jungle", centre: "supercentre", img: "x"}
];
}]);
The javascript that I use to update the scope from outside my angular app. It is a promise javascript function to handle ajax request
var data = load_content(target).then(function (response) {
// console.log('Target -> ' + target);
data = response;
var scope = angular.element($("#object_container")).scope();
scope.$apply(function(){
console.log(scope.objects);
scope.objects = data[1];
console.log(scope.objects);
});
data[1] is built with php like this;
$data = array();
$data[0] = "OK"; // status from server OK/ERROR
$data[1][0]->id = "4";
$data[1][0]->name = "route Angulaire";
$data[1][0]->cote = "10.5";
$data[1][0]->style = "jungle";
$data[1][0]->centre = "some center";
** Load content JS function **
function load_content(target){
return new Promise(function (resolve, reject) {
$.ajax({
url: '/ajax-processor.php',
type: 'POST',
data: { 'ajaxKey':'MyKey' ,'target': target },
success: function (data, status) {
resolve(data);
console.log(data);
if(data[0] == 'ERROR'){ // display error
$('#error_frame').removeClass("hidden");
$('#error_frame').html(data[1]);
}
else if(data[0] == 'OK'){ // we proceed
}
},
error: function (xhr, desc, err) { // sinon il y a erreur
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
return;
}
});// end ajax call
});
}
Im am calling load content from this jquery event listener
$(document.body).ready(function () {
$(document.body).on("click", "a", function (event) {
event.preventDefault(event);
// console.log('Clicked a link');
var id = ($(this).attr("id"));
var target = ($(this).attr("data-target"));
var link = ($(this).attr("href"));
var text = ($(this).text());
var html = ($(this).html());
/// vérification necessaire
///
//$(container).addClass("visible");
var data = load_content(target).then(function (response) {
// console.log('Target -> ' + target);
data = response;
var scope = angular.element($("#object_container")).scope();
scope.$apply(function(){
console.log(scope.objects);
scope.objects = data[1];
console.log(scope.objects);
});
});
$(target).html('<img scr="/images/html/ajax-load.gif"');
}); // end on click a
}); // end document ready
Whats missing?
Thank you