I'm a bit of an Angular newbie so sorry if this is obvious.
I have a backend which returns an array of objects. After I retrieve them, I move to the next page and i need to display a checkbox for each of the objects. I can successfully retrieve them, so I change the location of browser to the next page where the checkboxes should be displayed but nothing is being displayed. I tried using $scope.$apply() however it throws an error stating that a digest is already taking place but I think that's correct because I've used ng-click which is wrapped in a $scope.$apply() I think?
I can see the services being printed out in the console successfully, however they don't appear in the page. Not sure what I'm doing wrong here?
In my starting view:
<form>
<div class="form-group">
<select id="main_service" name="main_service" class="form-control" ng-model="mainService">
<option>Please Select...</option>
<option ng-repeat="service in mainServices" value="{[{service}]}">{[{service}]}</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit" ng-click="updateServices()">Continue</button>
</div>
</form>
in my controller: var professionalSignupControllers = angular.module('professionalSignupCtrls', [])
professionalSignupControllers.controller('professionalQuestions', function($scope, $http, Service, $sce, $routeParams,$location){
$scope.mainService = "";
$scope.services = [];
$scope.updateServices = function(){
Service.getServices($scope.mainService)
.success(function(data) {
$scope.services = data
console.log($scope.services)
//$scope.$apply(); throws digest error!
$location.path( '/services' );
})
.error(function(data, error) {
event.preventDefault();
alert("Oh Dear")
});
};
})
In my services partial:
<div>
<div ng-repeat="service in services" class="checkbox">
<label><input type="checkbox" name="service[]" value="{[{service.id}]}" ng-model="user.services">{[{service.label}]}</label>
</div>
</div>
My servicesService (bad name):
angular.module('servicesService', [])
.factory('Service', function($http) {
return {
getServices : function(tag) {
return $http.get('/api/service?searchTerm='+tag)
}
}
});
My app config:
app.config(['$routeProvider', '$locationProvider','$interpolateProvider',
function($routeProvider,$locationProvider,$interpolateProvider) {
$routeProvider.
when('/start', {
templateUrl: '/js/partials/start-professional-questions.html',
controller: 'professionalQuestions'
}).
when('/services', {
templateUrl: '/js/partials/services-professional-questions.html',
controller: 'professionalQuestions'
}).
otherwise({
redirectTo: '/start'
});
$interpolateProvider.startSymbol('{[{').endSymbol('}]}');
}
]);
I'm using {[{ }]} for interpolation as otherwise it would class with the Blade syntax