Currently, I am still in the process of familiarizing myself with Angularjs and actively learning about its functionalities.
One issue I have encountered is that when I define an array and loop through it using "ng-repeat," the items within ng-repeat fail to update after a function is executed. An example of this can be seen in the following jsfiddle.
http://jsfiddle.net/ematevosyan/t3ruzytk/8/
function MyCtrl($scope) {
// initializing google maps auto complete input
var options = {
types: ['(cities)']
},
input = document.getElementById('searchTextField'),
autocomplete = new google.maps.places.Autocomplete(input, options);
// sample array
$scope.testArray = [{
name: 'item1'
}];
// function to test a push into array
$scope.testPush = function () {
$scope.testArray.push({
name: 'item2'
});
}
// change listener for google input autocomplete
$scope.inputChanged = google.maps.event.addListener(autocomplete, 'place_changed', function () {
// storing the location in a variable
$scope.specificLocation = autocomplete.getPlace().formatted_address;
//alert($scope.specificLocation);
$.ajax({
type: 'GET',
url: 'https://api.github.com/repos/bvasko/Bootstrap-Select-Box',
async: false,
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
alert(data);
$scope.testArray.push({
name: $scope.specificLocation
});
},
error: function (e) {
console.log(e.message);
}
}); // end ajax call
});
}
In the provided example, you can observe that the button functions correctly by pushing a new item into the array and updating the ng-repeat. However, after entering a location in the input field and updating the array, the new item is added to the array but does not reflect in the ng-repeat. I am striving to achieve real-time updates in the ng-repeat as soon as a user inputs a location.
I have attempted solutions such as utilizing Angular's $http instead of $ajax and implementing $apply, but unfortunately, neither of these approaches yielded the desired outcome. I suspect there might be a simple solution that I am overlooking, but I am unable to pinpoint it at the moment.