1

I am working in ionic and angular. I want to set variables in controller and use them in view. I am using cookies to save current logged in user information. Here is the controller code:

.controller('profileCtrl',['$http','$scope', 
'fileUpload','$cookies','$cookieStore','$window',
 function($http, $scope,    fileUpload,$cookies,$cookieStore,$window) {
    $scope.CurrentUser = $cookieStore.get('CurrentUserInfo');<br/>
    $scope.CurrentUserFullname = $scope.CurrentUser.User.fullname;<br/>
    console.log($scope.CurrentUserFullname); **// working. Suppose name is Simerjit
}])

Now in view profile.html. I want to display the name. I use this:
<h1>{{CurrentUserFullname}}</h1> // not working

Anyone please help. I am new in Ionic and angularjs.

3
  • 1
    Define "not working". What happens precisely? Also, is the <h1> in a section of the DOM that is handled by the controller? Post all the relevant code. Commented Apr 2, 2016 at 7:38
  • most likely you did some typo in naming controller in the template, please check your template correctly. <div ng-controller="profileCtrl"> //you should have this somewhere. or if you use a $routeProvider, check there. Commented Apr 2, 2016 at 7:47
  • I check $routeProvider and it is working fine. Commented Apr 2, 2016 at 7:58

5 Answers 5

0

I checked the code .That is right but you need to check value of fullname in

$cookieStore.get('CurrentUserInfo');

may be fullname has not any value .

I have made many project in ionic you are doing the right way.

{{CurrentUserFullname}}

Sign up to request clarification or add additional context in comments.

1 Comment

Basically fullname returns me null. Thanks for the reply.
0

You need to define the controller for the profile.html for example <div ng-controller="profileCtrl">

2 Comments

I am using $routProvider file. So it is binding controller and view file automatically. I don't need to specify ng-controller again. Am I right?
What errors do you get? Are you also loading the angular-route.js and the your-named-controller.js?
0

if you store user data direcly in cookie then not need it. $scope.CurrentUser.user.fullname

you can use directly this.

   $scope.CurrentUserFullname = $scope.CurrentUser.fullname;

1 Comment

Thanks. Basically it didn't displayed the fullname. Now issue is fixed. Thanks for your reply.
0

If your Data Binding not working properly try to use $route resolve to get the data into the controller before the view loads .. In your $route config ,

    $routeProvider
    .when("/profile", {
    templateUrl: "profile.html",
    controller: "profileCtrl",
    resolve: {
        CurrentUser: function($cookieStore){
            return $cookieStore.get('CurrentUserInfo');
    }
}})

and in Controller inject CurrentUser

    .controller('profileCtrl',['$http','$scope','CurrentUser' fileUpload','$cookies','$cookieStore','$window', function($http, $scope,CurrentUser,fileUpload,$cookies,$cookieStore,$window) {
       $scope.CurrentUser = CurrentUser;
       $scope.CurrentUserFullname = CurrentUser.User.fullname;
       console.log($scope.CurrentUserFullname); }])

for more info on resolve link

happy coding ...

Comments

-3

Controller shouldn't know about app's state. You should read about angular's rules and best practices there: https://github.com/johnpapa/angular-styleguide

1 Comment

Any relation between question and answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.