So I would have thought this would be easy however I am obviously doing something wrong, no matter what I do I can't get the "personId" variable in the conttroller when I change state. I can see it in the URL so you think it would be OK but it's always undefined. Hopefully someone can see the mistake - here is the config for the state
$stateProvider.state('quickScreen', {
params: {
hideAjaxContainer: "true",
personId: {}
},
//url: "/Components/QuickScreen/{personId}",
url: "/Components/QuickScreen/:personId",
views: {
angularContentContainer: {
templateUrl: "/Components/QuickScreen/index.html",
controller: "quickScreenController",
resolve: {
name: function ($stateParams) {
return $stateParams.personId;
}
}
}
}
});
and this is the controller
var quickScreenController = function ($scope, $stateParams) {
console.log("quickScreenController loaded: " + $stateParams.personId);
};
It is possible that because my URL's end up looking like this "http://localhost:1790/Search2#/Components/QuickScreen/5a114c4f-44c2-477a-97e5-a19500995016" it doesn't work but I don't know. Thanks...
I do not see any bad in your syntax. Even more, I created working plunker here and this is working
$stateProvider.state('quickScreen', {
params: {
hideAjaxContainer: "true",
personId: {}
},
//url: "/Components/QuickScreen/{personId}",
url: "/Components/QuickScreen/:personId",
views: {
angularContentContainer: {
templateUrl: 'tpl.html',
//templateUrl: "/Components/QuickScreen/index.html",
controller: "quickScreenController",
resolve: {
name: function($stateParams) {
return $stateParams.personId;
}
}
}
}
});
Check it here. If this still won't help, update that plunker to reproduce the issue... ready to assist...
Related
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.
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.
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);
}
}
})
My state definition looks like:
.state('repository', {
url: '/repository/:host/:owner/:repository',
views: {
appView: {
templateUrl: '/templates/app/repository.html'
},
'repositoryView#repository': {
templateUrl: '/templates/app/_repositoryAnalytics.html'
}
},
resolve: {
resolvedRepository: [
'$stateParams', 'RepositoryService', function($stateParams, RepositoryService) {
console.log(RepositoryService.find($stateParams.host, $stateParams.owner, $stateParams.repository));
return 1;
}
]
},
controller: 'AnalyticsController'
});
However, it hands and never gets to the AnalyticsController. If I remove the resolve, then it works just fine (except, of course, I don't have the resolved value).
I did return 1 for now just to see if it would return something without worrying about promises. I did the console.log to see if it hit, and it does not.
I created update of the prvious plunker here. All is working if all the parts are as well. Your resolve code is ok (it could really return just a value). The only thing different is:
The controller belongs to the view === { views : { viewName : { template, controller}}}
Controller should not (cannot) be defined at the state level. It simply is there to do the MVC stuff...
$stateProvider.state('repository', {
url: '/:host/:owner/:repository',
views: {
appView: {
template: '<div><h1>Repository</h1><div ui-view="repositoryView" ></div>',
controller: 'AnalyticsController', // here is new position
}
},
// removed from here
// controller: 'AnalyticsController',
resolve: {
resolvedRepository: [
'$stateParams', 'RepositoryService', function($stateParams, RepositoryService) {
console.log(RepositoryService.find($stateParams.host, $stateParams.owner, $stateParams.repository));
return 1;
}
]
},
Check it in action here
If you want to return a value for testing purposes, you need to do it in an object, see the first example here: https://github.com/angular-ui/ui-router/wiki#resolve
resolve: {
resolvedRepository: [
'$stateParams',
'RepositoryService',
function($stateParams, RepositoryService) {
return {'value': 1};
}
]
}
If that won't work, try leaving out your RepositoryService to check if that's the problem:
resolve: {
resolvedRepository: [
'$stateParams',
function($stateParams) {
return {'value': 1};
}
]
}
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.