Sorry if this question already exists but i could not find it.
I'm working on an Angular project, and I have loaded a external JSON file using http. This data is displayed using ngRepeat.
I would like to load the file again using different parameters when a button is clicked and update ngRepeat
JavaScript:
angular.module('App', [])
.controller('ResultsCtrl', function($scope, $http) {
$http.get(searchUrl).then(function(res){
$scope.markersAll = res.data;
})
.success(function(data, status, headers, config) {
$scope.markersAll = res.data;
})
.error(function(data, status, headers, config) {
// something went wrong
});
});
HTML
<div ng-repeat="marker in markersAll">
<h4>{{marker.org}}</h4>
</div>
<select id="Category_Selection">
...
</select>
<input type="button" onClick="searchLocations()" value="Search"/>
I have tried wrapping the JS in a function an calling it when the button is click, but that didn't work. How can I update $scope.MarkersAll using a new http GET request? Or is there a different way I should be loading this data?
edit: updated to use ' ng-click
' instead of ' onClick
'.
Btw, it's a good practice to use controllerAs syntax instead of binding directly to $scope.
angular .module('App', []) .controller('ResultsCtrl', function($scope, $http) { $scope.markersAll = []; //initialize to empty array so ng-repeat doesn't complain $scope.searchLocations = searchLocations; searchLocations(); //perform a search on load function searchLocations() { $http.get(searchUrl).then(function(res) { $scope.markersAll = res.data; }); } });
<div ng-repeat="marker in markersAll"> <h4>{{marker.org}}</h4> </div> <select id="Category_Selection"> ... </select> <input type="button" ng-click="searchLocations()" value="Search" />
In your angular js code
you can try following:
angular.module('App', [])
.controller('ResultsCtrl', function($scope, $http) {
$http.get(SITE_URL).success(function(data) {
$scope.markersAll=data;
});
$scope.myproject = function(marker) {
$http.get(SITE_URL?marker="+marker).success(function(data){
$scope.markersall1=data;
});
And in your html
code you can add this:
<div ng-repeat="marker in markersAll">
<h4>{{marker}}</h4>
</div>
<div ng-repeat="marker in markersAll1">/*data come using ajax on button click event*/
<h4>{{marker}}</h4>
</div>
<select ng-model="marker">
<option ng-repeat="marker in markersAll">{{marker}}</option>
</select>
<button type="button" ng-click="myFunction(marker)">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.