UI-route nested views and resolve not working - angularjs

I have a problem with resolve and my nested view is not loading because ot that. I don't see where is the problem. This was working with ng-route.
Here is my situation. We should see "View3" text bellow other tow. If you remove the resolve it will work.
[http://plnkr.co/edit/p483wWVSp30LGiQMuk1r?p=preview][1]
[1]: http://plnkr.co/edit/p483wWVSp30LGiQMuk1r?p=preview

Here is my State with Resolve. The problem is that i used $route as parameter. $route is averrable for ng-route module.
.state('test.subs.quest', {
url: '/:bob',
views: {
'quest#test': {
template: "View3",
controller: function($stateParams) {
console.log($stateParams);
}
},
},
resolve: {
Questions: function(Questionnaire, $route) {
console.log('any body here ?');
return Questionnaire.Get($route.current.params.slug);
}
}
});
and it should be like this:
.state('categories.category.questionnaire', {
url: '/:slug',
views:{
"main#":{
template: '<div compilehtml="html"></div>',
controller: 'QuestionnaireController',
}
},
resolve: {
Questions: function(Questionnaire, $stateParams ) {
return Questionnaire.Get($stateParams.slug);
}
}
})

Related

angularJS $stateProvider - resolve $scope

I'm trying to resolve $scope for component-usage reasons but it seems to be a problem... please look at the code and let me know if you see a problem or have a different idea of how to implement it.
angular.module('app').config(
function ($stateProvider) {
$stateProvider.state('home', {
url: '/home',
views: {
'': {
template: `<my-comp user-logged-in="userModel"></my-comp>`
}
},
resolve: {
'$scope': function ($rootScope) {
var scope = $rootScope.$new();
scope.userModel = {userId:1, name:'user'};
return scope;
}
}
})
});
As you can see I'm trying to pass the userModel's data directly to the component controller, but it fails...
Currently I'm using the following logic which work's fine but i'm trying something new :)
views: {
'': {
template: `<my-comp user-logged-in="$ctrl.userModel"></my-comp>`,
controller: function (userLoggedIn) {
this.userModel = userLoggedIn;
},
controllerAs: '$ctrl'
}
},
resolve: {
userLoggedIn: function (userLoggedIn) {
return userLoggedIn;
}
}
Please advise.

How to inject a resolve into a directive with Angular UI-Router State?

I am using ui-router in the following format to load a directive. the controller is not specified in the state but inside the directive itself.
.state('app.main', {
url: '/main',
template: '<my-directive>',
data: {
dataName1: 'dataVal1',
dataName2: 'dataVal2',
}
})
this works well for most cases. however, I am not able to inject a resolve to the directive this way. I have tried the following but keep getting a 'unknown provider' error inside the directive.
.state('app.main', {
resolve: {
simpleObj: function () {
return { value: 'simple!' };
}
},
url: '/main',
template: '<my-directive>',
data: {
dataName1: 'dataVal1',
dataName2: 'dataVal2',
}
})
Is there a way to make the resolve work with this scenerio?
Try the below code:
resolve: {
demoResolve: ['myResolvingService', function(resolver) {
resolver.myValue = 'Foo';
return '`enter code here`Foo';
}]

Share resolve in Angular UI Router

I have various states that use the same resolve to load a timeLog into my controller $scope before the controller loads. I would like to not reproduce this code but share it between these views. I'm fairly new to JS frameworks, and especially Angular JS.
I'm having a hard time googling this and not finding any decent information. Maybe I'm searching incorrectly or not thinking about this correctly. Any Suggestions?
.config(function($stateProvider) {
$stateProvider
.state('tab.edit-log-summary', {
url: '/logs/edit-log-summary/:timeLogId',
views: {
'tab-logs': {
templateUrl: 'templates/logs/edit-log-summary.html',
controller: 'EditLogSummaryCtrl'
}
},
resolve: {
timeLog: function(config, $stateParams, DailyLog) {
return DailyLog.get({id: $stateParams.timeLogId});
},
}
})
.state('tab.edit-time-log', {
url: '/logs/edit-time-log/:timeLogId',
views: {
'tab-logs': {
templateUrl: 'templates/logs/edit-time-log.html',
controller: 'EditTimeLogCtrl'
}
},
resolve: {
timeLog: function(config, $stateParams, DailyLog) {
return DailyLog.get({id: $stateParams.timeLogId});
},
}
})
})
This really goes down to vanilla Javascript. The resolves are objects. Just define them as a single object somewhere above and pass it to the resolve property each time.
.config(['$stateProvider',function($stateProvider) {
var timeLogResolve = {
timeLog: ['config','$stateParams','DailyLog',function(config, $stateParams, DailyLog) {
return DailyLog.get({id: $stateParams.timeLogId});
}]
};
$stateProvider
.state('tab.edit-log-summary', {
url: '/logs/edit-log-summary/:timeLogId',
views: {
'tab-logs': {
templateUrl: 'templates/logs/edit-log-summary.html',
controller: 'EditLogSummaryCtrl'
}
},
resolve: timeLogResolve,
}
})
.state('tab.edit-time-log', {
url: '/logs/edit-time-log/:timeLogId',
views: {
'tab-logs': {
templateUrl: 'templates/logs/edit-time-log.html',
controller: 'EditTimeLogCtrl'
}
},
resolve: timeLogResolve,
}
})
}])
One suggestion- use inline array notation for providing dependencies. This helps protect your code from breaking if you minify it. I did that myself in the demo above, but I leave it to your discretion to keep it.

Testing values returned from resolve object in ui-router state

I have a UI-router state with some resolves, and I now want to test the output of these resolves, but I have no idea how to get access to the results.
I tried mocking the controller and check the arguments passed into it, but I seem to be unable to do this.
route:
$stateProvider.state('home', {
url: '/',
views: {
content: {
controller: 'HomeCtrl as vm',
template: ''
}
},
resolve: {
myresolve: function() { return 'cool!'; }
}
});
test:
$state.go('home');
$rootScope.$apply();
expect(???).toEqual('cool!');
In your controller HomeCtrl inject myresolve :
angular.module(...).controlle('HomeCtrl', function(myresolve){
expect(myresolve).toEqual('cool!');
});
cf : https://github.com/angular-ui/ui-router/wiki#resolve
or : https://scotch.io/tutorials/making-skinny-angularjs-controllers#use-ui-router-to-state-the-obvious-

angular-ui-router views and controllers are not loading after upgrading to angular 1.3

after I did the update to angular 1.3 I'm not able to reach the controllers and views, they simply doesn't load.
the states in my root works perfectly, this is my app.js
$stateProvider.state("root",
{
url: '',
abstract: true,
views: {
'footer#': {
templateUrl: "/partial/footer/footer.html",
},
'header#': {
templateUrl: "/partial/header/header.html",
}
}
}).state('root.home', {
url: '/index',
views: {
'container#': {
templateUrl: '/partial/index/index.html',
controller: 'IndexCtrl'
}
}
}
).state('root.welcome', {
url: '/index/:status',
views: {
'container#': {
templateUrl: '/partial/index/index.html',
controller: 'IndexCtrl'
}
}
});
an also my configuration:
$urlRouterProvider.otherwise('index');
$locationProvider.hashPrefix('!');
$locationProvider.html5Mode({ enabled: false });
by after doing a stage.go or just by typing the url, I'm not able to reach any route in these states:
$stateProvider.state("candidate",
{
url: '',
abstract: true,
views: {
'footer#': {
templateUrl: "/partial/footer/footer.html"
},
'header#': {
templateUrl: "/user/partial/user.header/user.header.html",
},
'sideBar#': {
templateUrl: '/user/partial/user.sidebar/user.sidebar.html',
controller: 'SidebarCtrl',
resolve: {
user: 'currentUser'
}
},
'navBar#': {
templateUrl: '/user/partial/navbar/navbar.html'
}
}
}).state('candidate.dashboard',
{
url: '/dashboard',
views: {
'container#': {
templateUrl: '/user/partial/user.dashboard/user.dashboard.html',
controller: 'DashboardCtrl',
resolve: {
dashboardinfo: function ($resource, tracker) {
var resourceGet = $resource('/user/dashboard');
var promise = resourceGet.get().$promise;
tracker.addPromise(promise);
return promise;
}
}
}
}
})
I've spent a couple of hours trying to figure this out without any luck, maybe it's just a small detail I'm missing, any advice will be more than welcome.
PS: I'm using v0.2.12-pre1
I tried to replicate the issue, mentioned above in this working plunker. All the code is almost unchanged, I just used a custom version of UI-Router. The reason is this reported and known bug:
Impossible to disable html5Mode with angular 1.3.0-rc.3 #1397 (small cite:)
Due to: angular/angular.js#dc3de7f
the html5mode-check in urlRouter.js [line 383] is no longer correct. And thus it's impossible to disable html5mode.
A quick fix could be:
var isHtml5 = $locationProvider.html5Mode();
if (angular.isObject(isHtml5)) {
isHtml5 = isHtml5.enabled;
}
And this (the code above) is the change I made. Nothing else. All the code started to work then...
Check also this answer
ui-router not producing correct href attribute in angular
for a Chris T link to fixed version...
it turns out that the resolve method was failing, I'm using angular promise tracker
var resourceGet = $resource('/user/dashboard');
var promise = resourceGet.get().$promise;
tracker.addPromise(promise); <-- fails here
return promise;
it seems like resources and promises have breaking changes and my implementation could be deprecated.

Resources