1
(function (angular) {
'use strict';

angular
    .module('app')
    .controller('ListSharedContentCtrl', ['$scope','$log','UserService', 'ContractService','ContentOwnerService','PlatformPartnerService',function ($scope, $log, userService, contractService,contentOwnerService,platformPartnerService) {
        $scope.init = function () {
            var contractIds =[];
           var contentOwnerId;
            var platformPartnerId;
            var user=userService.getCurrentUser('true');
            var contentOwner=userService.isCurrentUserIsContentOwner();
            var platformPartner=userService.isCurrentUserIsPlatformPartner();
            if(contentOwner == "true")
            {
                contentOwnerService.getContentOwnerId().then(function(savedContentOwnerId) {

                 contentOwnerId=savedContentOwnerId;
                 console.log(contentOwnerId);//here I can log the value

                },function(error) {
                    $log.error("Error fetching contract id:" + error.message);
                });
            }
        console.log(contentOwnerId); //but Here I cant log the value..Its showing undefined  
       } $scope.init();
    }]);
  })(angular);

Now my question is how to make the scope of the variable "contentOwnerId" available to whole function?Please anyone help me I cant figure it out..Thanks in advance..!

3
  • You're missing a closing } right after your console.log Commented Apr 21, 2015 at 2:53
  • 1
    you mean $scope.contentOwnerId like this? Commented Apr 21, 2015 at 2:53
  • Yes how to make variable's scope available to whole init() method Commented Apr 21, 2015 at 4:24

4 Answers 4

2

You are declaring contentOwnerId twice; once inside the if block, once outside it. The one outside is declared, but never assigned a value. It should be null where it is showing null.

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

3 Comments

if I doesnt decalre again will it retain the stored value
I have made the cahnges as you said but still its not working
I can see only one declaration of contentOwnerId, and then, inside the resolved promise, savedContentOwnerId is assigned to it.
1

The reason why you are getting the contentOwnerId undefined is that it is undefined until when the getcontentOwnerId() promise succeed!

if you do

var contentOwnerId = "before resolving the promise"; 

at the beginning of your init() you will probably have that string logged to console, instead of undefined

Comments

0

use $scope.contentOwnerId instead of var contentOwnerId.

See $scope.contentOwnerId is available to whole controller i.e ListSharedContentCtrl.

normal javascript variable are limited to functions where as $scope is available to entire CONTROLLER

Comments

0

The .then() method belongs to a promise. This means it won't be executed immediately, but later in the angular life cycle.

Your console.log() will always log 'undefined' because your variable won't be initialized there. You can try this to log the value:

$scope.$watch( 
    function() { return contentOwnerId; }, 
    function(newContentOwnerId, oldContentOwnerId) {
        console.log(newContentOwnerId);
    }
);

1 Comment

Where to implement this code..I understood that the value may not be initialized early because I m resolving it from a promise.Is there any method to bind the value to contentOwnerId as soon as the promise is resolved..?

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.