This is my code:
Service Module
(function () {
var servicesSushi = angular.module('SushiServices', ['ngResource']);
servicesSushi.factory('Data', ['$resource',
function ($resource) {
return $resource('Data/:name.json', {}, {
getInfoMenu: { method: 'GET', params: { name: 'sushiMenu' }, isArray: true },
getInfoPlaces: { method: 'GET', params: { name: 'sushiPlaces' }, isArray: true }
});
}]);
})();
In my app.config
(function () {
var app = angular.module('Sushi', ['ngRoute','SushiServices', 'SushiControllers']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/menu', {
templateUrl: 'partials/_menu.html',
controller: 'MenuController'
}).
when('/places', {
templateUrl: 'partials/_places.html',
controller: 'PlacesController'
}]); //I have removed the other .whens and the otherwise for simplicity.
})();
My controlls module
(function () {
var SushiAppControllers = angular.module('SushiControllers', ['ngSanitize']);
SushiAppControllers.controller('MenuController', ['$scope', 'Data',
function ($scope, Data) {
$scope.pos = 1;
$scope.dataMenu = Data.getInfoMenu();
$scope.setPos = function (value) {
$scope.pos = value;
};
$scope.isPos = function (value) {
return $scope.pos === value;
};
}]);
SushiAppControllers.controller('PlacesController', ['$scope', '$sce', 'Data',
function ($scope, $sce, Data) {
$scope.pos = 1;
$scope.dataPlaces = Data.getInfoPlaces();
$scope.urlMap = function () {
return $sce.trustAsResourceUrl($scope.dataPlaces[$scope.pos - 1].map);
};
$scope.setPos = function (value) {
$scope.pos = value;
};
}]);
})();
My problem: When I first try to visit the view associated with places, nothing shows; then, whenever I visit it again, everything works fine. I think I get that part, the view is ready before the data arrives, right? The thing is, I don't want the user to have to click a button twice in order to get results, how can I prevent that from happening?
Also, please explain the resolve property in the simplest way you can possibly find.
Side note, getInfoMenu
throws Invalid Token Exception
. I've checked thoroughly, my json is lengthy but valid. BTW Sorry if all that sushi made you hungry.
I'm sorry if this seems duplicated, but I can't really understand this resolve
thing. I've read the blog with the three stories, and some of the questions, e.g. this, this, this; but I haven't been able to understand any of the approaches.
EDIT Actually, all of my views take two clicks before they load correctly. Let me explain further, I have a navbar, I click the first button, nothing happens, go to second button, nothing happens go to third and fourth button, nothing happens. But when I return to the first and so forth after the first click, it works! The Unexpected Token
thingy is still happening, though.