Reputation: 55273
In the following Angular controller, the $q.all
gets a JSON tree which is saved in $scope.building
. $scope.building
is then used to create pano
's and panodata
's:
JS:
angular.module('yoApp')
.controller('addPanoCtrl', function($q, $scope, $rootScope, $routeParams, serviceUpload) {
$scope.buildingId = $routeParams.buildingId
$q.all([
serviceUpload.getAllBuilding()
]).then(function(value) {
var buildingJSON = _.where(JSON.parse(value), {'objectId': $scope.buildingId})
$scope.building = buildingJSON[0]
}, function(reason) {
$scope.result = reason
})
// Methods
$scope.addPano = function() {
// SOME CODE
pano.save(json, {
success: function(object) {
$scope.building.pano.push(json)
$scope.panoName = ''
$scope.$digest()
},
error: function(object, error) {
}
})
}
$scope.addPanodata = function(panoId) {
// SOME CODE
panodata.save(json, {
success: function(object) {
$scope.pano.panoData.push(json)
$scope.panodataName = ''
$scope.$digest()
},
error: function(object, error) {
}
})
}
})
HTML:
<div class="col-md-3" ng-repeat="pano in building.pano">
<!-- SOME CODE -->
<a class="btn btn-primary" href="javascript:;" ng-click="addPanodata(pano.objectId)">Add</a>
Saving pano
's has no problem since I have $scope.buildingId
. The problem arises when I save a panodata
. If I create a pano
and then a panodata
I can't since $scope.building
doesn't have the new pano
.
The panodata
is saved if I reload the page, but I was thinking if there's away to reload only the $q.all
bit so $scope.building
get's fetched again with the newly created pano
.
What's the common way to accomplish this in AngularJS?
Upvotes: 0
Views: 59
Reputation: 3261
You can extract getting the data into a function and then call it again when you need to update the data, e.g. inside of $scope.addPanodata()
Upvotes: 1