Posts

Showing posts with the label Javascript Framework

Angular JS Break ForEach

Image
Answer : The angular.forEach loop can't break on a condition match. My personal advice is to use a NATIVE FOR loop instead of angular.forEach . The NATIVE FOR loop is around 90% faster then other for loops. USE FOR loop IN ANGULAR: var numbers = [0, 1, 2, 3, 4, 5]; for (var i = 0, len = numbers.length; i < len; i++) { if (numbers[i] === 1) { console.log('Loop is going to break.'); break; } console.log('Loop will continue.'); } There's no way to do this. See https://github.com/angular/angular.js/issues/263. Depending on what you're doing you can use a boolean to just not going into the body of the loop. Something like: var keepGoing = true; angular.forEach([0,1,2], function(count){ if(keepGoing) { if(count == 1){ keepGoing = false; } } }); please use some or every instances of ForEach, Array.prototype.some: some is much the same as forEach but it break when the callback returns true Array.prototype.e...

AngularJS Passing Data To $http.get Request

Answer : An HTTP GET request can't contain data to be posted to the server. However, you can add a query string to the request. angular.http provides an option for it called params . $http({ url: user.details_path, method: "GET", params: {user_id: user.id} }); See: http://docs.angularjs.org/api/ng. http#get and https://docs.angularjs.org/api/ng/service/ http#usage (shows the params param) You can pass params directly to $http.get() The following works fine $http.get(user.details_path, { params: { user_id: user.id } }); Starting from AngularJS v1.4.8 , you can use get(url, config) as follows: var data = { user_id:user.id }; var config = { params: data, headers : {'Accept' : 'application/json'} }; $http.get(user.details_path, config).then(function(response) { // process response here.. }, function(response) { });