jadjoubran / laravel5-angular-material-starter

Get started with Laravel 5.3 and AngularJS (material)
https://laravel-angular.readme.io/
MIT License
1.66k stars 401 forks source link

How to get ui-router resolved values in controller #474

Closed dinesh2017 closed 7 years ago

dinesh2017 commented 7 years ago

router.config.js

  $stateProvider.state('app.user-datshboard', {
        url: '/dashboard/:user',
        views: {
            'header@':{
                templateUrl : getView('user-navbar')
            },
            'main@': {
                templateUrl: getView('user-dashboard')
            },
            'footer':{}
        },
        params: {
            user : null
        },
        resolve: {
            OurUser : (UserServicesService)=>{
                return UserServicesService.checkUser();
            }
        }
    })

UserServices.service.js

  export class UserServicesService{
   constructor(API,$auth,$log){
      'ngInject';
    this.API = API;
    this.$log = $log;
    this.$auth = $auth;
    this.user = null;
  }
   checkUser()
  {
    if(this.$auth.isAuthenticated()) {
        this.API.one('isProfile').get().then((response)=>{
            if(response.data.user == null)
                this.user = response.data.user;
        });
        return this.user;
     }
  }
 }

user-dashboard.component.js 

class UserDashboardController{
constructor(DialogService,API,$log,$timeout,$state,$stateParams,$auth,$scope){
    'ngInject';
    //
    this.$log = $log;
    this.$log.log(this.OurUser);
}

}

 export const UserDashboardComponent = {
  templateUrl: './views/app/components/user-dashboard/user-dashboard.component.html',
  controller: UserDashboardController,
   controllerAs: 'vm',
   binding : {'OurUser: '='}
 }

Now, i'm getting undefined with "OurUser" variable.

0xGeegZ commented 7 years ago

HI,

Did you try with a promise in your service?

dinesh2017 commented 7 years ago

Yes i tried. But i'm getting always undefined...

yuri-novikov commented 7 years ago

You got typo datshboard in 1 line $stateProvider.state('app.user-datshboard', {

maybe that is the point?

DedSec49 commented 7 years ago

In your file There is a typo on a line: 12 It should be re-written as:

this.API.one('isProfile').get('').then((response)=>{

Hope this helps.