How to add a $scope object value to another $scope object? (AngularJS)












0















I'm taking the data from different sharepoint pages lists. I'm calling these lists with a Factory.

My code is counting how many items with a "Completed" status each list has.

I need to take the values of each one into an array, but the result is always null.

Here's my example:



    <script>
var myApp = angular.module("myApp", );
myApp.factory("myFactory", ["$http", function($http) {
return {
siteOne: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 1')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
},
siteTwo: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 2')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
}
}
}]);




        myApp.controller("myController", function($scope, $http, myFactory) {
myFactory.siteOne().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
});

myFactory.siteTwo().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
});

$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.oneItems));
console.log(JSON.stringify($scope.twoItems));
console.log(JSON.stringify($scope.data));
});
</script>


If I want to print each value separately, it shows the values! But if I try to put them inside the array, it shows the values as "null":



3
5
[null, null]


Why is this happening and how can I fix this? ..am I doing something wrong?



CODE UPDATE

Here is my code already working for those who'd like to see it. I changed the controller as Sergey Mell sugested, using $q, also I'm using AngularJS v1.7.5 (as georgeawg sugested):



myApp.controller("myController", function($scope, $http, myFactory, $q) {

$q.all([
myFactory.siteOne().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
}),
myFactory.siteTwo().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
})
]).then(function() {
$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.data));
});
});









share|improve this question




















  • 1





    The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

    – georgeawg
    Jan 2 at 5:49











  • I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

    – Soul Eeater
    Jan 2 at 23:07











  • Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

    – georgeawg
    Jan 3 at 4:11











  • Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

    – Soul Eeater
    Jan 3 at 16:45











  • The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

    – georgeawg
    Jan 3 at 17:09


















0















I'm taking the data from different sharepoint pages lists. I'm calling these lists with a Factory.

My code is counting how many items with a "Completed" status each list has.

I need to take the values of each one into an array, but the result is always null.

Here's my example:



    <script>
var myApp = angular.module("myApp", );
myApp.factory("myFactory", ["$http", function($http) {
return {
siteOne: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 1')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
},
siteTwo: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 2')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
}
}
}]);




        myApp.controller("myController", function($scope, $http, myFactory) {
myFactory.siteOne().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
});

myFactory.siteTwo().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
});

$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.oneItems));
console.log(JSON.stringify($scope.twoItems));
console.log(JSON.stringify($scope.data));
});
</script>


If I want to print each value separately, it shows the values! But if I try to put them inside the array, it shows the values as "null":



3
5
[null, null]


Why is this happening and how can I fix this? ..am I doing something wrong?



CODE UPDATE

Here is my code already working for those who'd like to see it. I changed the controller as Sergey Mell sugested, using $q, also I'm using AngularJS v1.7.5 (as georgeawg sugested):



myApp.controller("myController", function($scope, $http, myFactory, $q) {

$q.all([
myFactory.siteOne().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
}),
myFactory.siteTwo().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
})
]).then(function() {
$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.data));
});
});









share|improve this question




















  • 1





    The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

    – georgeawg
    Jan 2 at 5:49











  • I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

    – Soul Eeater
    Jan 2 at 23:07











  • Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

    – georgeawg
    Jan 3 at 4:11











  • Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

    – Soul Eeater
    Jan 3 at 16:45











  • The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

    – georgeawg
    Jan 3 at 17:09
















0












0








0








I'm taking the data from different sharepoint pages lists. I'm calling these lists with a Factory.

My code is counting how many items with a "Completed" status each list has.

I need to take the values of each one into an array, but the result is always null.

Here's my example:



    <script>
var myApp = angular.module("myApp", );
myApp.factory("myFactory", ["$http", function($http) {
return {
siteOne: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 1')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
},
siteTwo: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 2')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
}
}
}]);




        myApp.controller("myController", function($scope, $http, myFactory) {
myFactory.siteOne().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
});

myFactory.siteTwo().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
});

$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.oneItems));
console.log(JSON.stringify($scope.twoItems));
console.log(JSON.stringify($scope.data));
});
</script>


If I want to print each value separately, it shows the values! But if I try to put them inside the array, it shows the values as "null":



3
5
[null, null]


Why is this happening and how can I fix this? ..am I doing something wrong?



CODE UPDATE

Here is my code already working for those who'd like to see it. I changed the controller as Sergey Mell sugested, using $q, also I'm using AngularJS v1.7.5 (as georgeawg sugested):



myApp.controller("myController", function($scope, $http, myFactory, $q) {

$q.all([
myFactory.siteOne().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
}),
myFactory.siteTwo().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
})
]).then(function() {
$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.data));
});
});









share|improve this question
















I'm taking the data from different sharepoint pages lists. I'm calling these lists with a Factory.

My code is counting how many items with a "Completed" status each list has.

I need to take the values of each one into an array, but the result is always null.

Here's my example:



    <script>
var myApp = angular.module("myApp", );
myApp.factory("myFactory", ["$http", function($http) {
return {
siteOne: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 1')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
},
siteTwo: function() {
return $http({
method: "GET",
url: "siteURL/_api/web/lists/getByTitle('List 2')/items",
headers: {"Accept": "application/json; odata=verbose"}
});
}
}
}]);




        myApp.controller("myController", function($scope, $http, myFactory) {
myFactory.siteOne().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
});

myFactory.siteTwo().success(function(data, status, headers, config) {
$scope.projects = data.d.results;
var items = $scope.projects,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
});

$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.oneItems));
console.log(JSON.stringify($scope.twoItems));
console.log(JSON.stringify($scope.data));
});
</script>


If I want to print each value separately, it shows the values! But if I try to put them inside the array, it shows the values as "null":



3
5
[null, null]


Why is this happening and how can I fix this? ..am I doing something wrong?



CODE UPDATE

Here is my code already working for those who'd like to see it. I changed the controller as Sergey Mell sugested, using $q, also I'm using AngularJS v1.7.5 (as georgeawg sugested):



myApp.controller("myController", function($scope, $http, myFactory, $q) {

$q.all([
myFactory.siteOne().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.oneItems = totalItems;
}),
myFactory.siteTwo().then(response => {
var items = response.data.d.results,
totalItems = 0;

for (var i=0;i<items.length;i++) {
var currentItem = items[i];
if(currentItem.Status!="Completed") {
totalItems++;
}
};
$scope.twoItems = totalItems;
})
]).then(function() {
$scope.data = [
$scope.oneItems, $scope.twoItems
];

console.log(JSON.stringify($scope.data));
});
});






javascript angularjs scope






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:40







Soul Eeater

















asked Jan 2 at 5:14









Soul EeaterSoul Eeater

7910




7910








  • 1





    The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

    – georgeawg
    Jan 2 at 5:49











  • I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

    – Soul Eeater
    Jan 2 at 23:07











  • Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

    – georgeawg
    Jan 3 at 4:11











  • Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

    – Soul Eeater
    Jan 3 at 16:45











  • The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

    – georgeawg
    Jan 3 at 17:09
















  • 1





    The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

    – georgeawg
    Jan 2 at 5:49











  • I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

    – Soul Eeater
    Jan 2 at 23:07











  • Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

    – georgeawg
    Jan 3 at 4:11











  • Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

    – Soul Eeater
    Jan 3 at 16:45











  • The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

    – georgeawg
    Jan 3 at 17:09










1




1





The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

– georgeawg
Jan 2 at 5:49





The .success method is deprecated. See Why are AngularJS $http success/error methods deprecated? Removed from v1.6?.

– georgeawg
Jan 2 at 5:49













I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

– Soul Eeater
Jan 2 at 23:07





I didn't know that, I've been using Angular v1.3.0. Thank for that info @georgeawg

– Soul Eeater
Jan 2 at 23:07













Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

– georgeawg
Jan 3 at 4:11





Both the request to site1 and the the request to site2 put their data on the same $scope vaariable, $scope.projects. Is that intentional?

– georgeawg
Jan 3 at 4:11













Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

– Soul Eeater
Jan 3 at 16:45





Yes, I made it intentional, but honestly I don't know why I did it.. right now it isn't giving me any problem but I've already modified it. I updated the code and is working.

– Soul Eeater
Jan 3 at 16:45













The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

– georgeawg
Jan 3 at 17:09







The .then method is available in AngularJS 1.3. In fact it has always been there. The .success method was created as a syntactic sugar but it is really a failure. It should be avoided in all new AngularJS code, including code intended for V1.3. V1.3 is no longer supported and using .success makes the migration path more difficult.

– georgeawg
Jan 3 at 17:09














3 Answers
3






active

oldest

votes


















1














You're putting your data before the requests have been finished. You have to wait till the end of both requests. I'd propose to use Promise.all or $q.all, which is more natural for AngularJS i.e.:



myApp.controller("myController", function($scope, $http, myFactory, $q) {
$q.all([
myFactory.siteOne().then(/* Your stuff here */),
myFactory.siteTwo().then(/* Your stuff here */),
]).then(function() {
$scope.data = [
$scope.oneItems, $scope.twoItems
];
})





share|improve this answer





















  • 1





    Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

    – georgeawg
    Jan 3 at 3:06











  • Good catch. Thank you for the qualification.

    – Sergey Mell
    Jan 3 at 8:27











  • I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

    – Soul Eeater
    Jan 3 at 16:43



















0














Also assign values to key like:



$scope.data = [
dataItemOne: $scope.oneItems,
dataItemTwo: $scope.twoItems ];





share|improve this answer































    -1














    You have best option as @Sergey Mell given below use $q service.

    If you don't want to use $q.
    then

    Declare $scope.data = ; at top and use
    $scope.data.push($scope.oneItems)

    But make sure you have declared $scope.data to at the top otherwise it will throw error.

    your controller code will be like



      myApp.controller("myController", function($scope, $http, myFactory) {
    $scope.data = ;
    myFactory.siteOne().success(function(data, status, headers, config) {
    $scope.projects = data.d.results;
    var items = $scope.projects,
    totalItems = 0;

    for (var i=0;i<items.length;i++) {
    var currentItem = items[i];
    if(currentItem.Status!="Completed") {
    totalItems++;
    }
    };
    $scope.oneItems = totalItems;
    $scope.data.push($scope.oneItems);
    });

    myFactory.siteTwo().success(function(data, status, headers, config) {
    $scope.projects = data.d.results;
    var items = $scope.projects,
    totalItems = 0;

    for (var i=0;i<items.length;i++) {
    var currentItem = items[i];
    if(currentItem.Status!="Completed") {
    totalItems++;
    }
    };
    $scope.twoItems = totalItems;
    $scope.data.push($scope.twoItems );
    });

    /* $scope.data = [
    $scope.oneItems, $scope.twoItems
    ];*/

    console.log(JSON.stringify($scope.oneItems));
    console.log(JSON.stringify($scope.twoItems));
    console.log(JSON.stringify($scope.data));
    });





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001517%2fhow-to-add-a-scope-object-value-to-another-scope-object-angularjs%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You're putting your data before the requests have been finished. You have to wait till the end of both requests. I'd propose to use Promise.all or $q.all, which is more natural for AngularJS i.e.:



      myApp.controller("myController", function($scope, $http, myFactory, $q) {
      $q.all([
      myFactory.siteOne().then(/* Your stuff here */),
      myFactory.siteTwo().then(/* Your stuff here */),
      ]).then(function() {
      $scope.data = [
      $scope.oneItems, $scope.twoItems
      ];
      })





      share|improve this answer





















      • 1





        Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

        – georgeawg
        Jan 3 at 3:06











      • Good catch. Thank you for the qualification.

        – Sergey Mell
        Jan 3 at 8:27











      • I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

        – Soul Eeater
        Jan 3 at 16:43
















      1














      You're putting your data before the requests have been finished. You have to wait till the end of both requests. I'd propose to use Promise.all or $q.all, which is more natural for AngularJS i.e.:



      myApp.controller("myController", function($scope, $http, myFactory, $q) {
      $q.all([
      myFactory.siteOne().then(/* Your stuff here */),
      myFactory.siteTwo().then(/* Your stuff here */),
      ]).then(function() {
      $scope.data = [
      $scope.oneItems, $scope.twoItems
      ];
      })





      share|improve this answer





















      • 1





        Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

        – georgeawg
        Jan 3 at 3:06











      • Good catch. Thank you for the qualification.

        – Sergey Mell
        Jan 3 at 8:27











      • I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

        – Soul Eeater
        Jan 3 at 16:43














      1












      1








      1







      You're putting your data before the requests have been finished. You have to wait till the end of both requests. I'd propose to use Promise.all or $q.all, which is more natural for AngularJS i.e.:



      myApp.controller("myController", function($scope, $http, myFactory, $q) {
      $q.all([
      myFactory.siteOne().then(/* Your stuff here */),
      myFactory.siteTwo().then(/* Your stuff here */),
      ]).then(function() {
      $scope.data = [
      $scope.oneItems, $scope.twoItems
      ];
      })





      share|improve this answer















      You're putting your data before the requests have been finished. You have to wait till the end of both requests. I'd propose to use Promise.all or $q.all, which is more natural for AngularJS i.e.:



      myApp.controller("myController", function($scope, $http, myFactory, $q) {
      $q.all([
      myFactory.siteOne().then(/* Your stuff here */),
      myFactory.siteTwo().then(/* Your stuff here */),
      ]).then(function() {
      $scope.data = [
      $scope.oneItems, $scope.twoItems
      ];
      })






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 2 at 6:52

























      answered Jan 2 at 5:53









      Sergey MellSergey Mell

      1,20511124




      1,20511124








      • 1





        Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

        – georgeawg
        Jan 3 at 3:06











      • Good catch. Thank you for the qualification.

        – Sergey Mell
        Jan 3 at 8:27











      • I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

        – Soul Eeater
        Jan 3 at 16:43














      • 1





        Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

        – georgeawg
        Jan 3 at 3:06











      • Good catch. Thank you for the qualification.

        – Sergey Mell
        Jan 3 at 8:27











      • I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

        – Soul Eeater
        Jan 3 at 16:43








      1




      1





      Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

      – georgeawg
      Jan 3 at 3:06





      Promise.all and $.all are not interchangeable. AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Only $q.all executes in the AngularJS context.

      – georgeawg
      Jan 3 at 3:06













      Good catch. Thank you for the qualification.

      – Sergey Mell
      Jan 3 at 8:27





      Good catch. Thank you for the qualification.

      – Sergey Mell
      Jan 3 at 8:27













      I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

      – Soul Eeater
      Jan 3 at 16:43





      I didn't know about $q, and for some reason this is working! Now the values in the array doesn't appear as "null". Thanks a lot for your help.

      – Soul Eeater
      Jan 3 at 16:43













      0














      Also assign values to key like:



      $scope.data = [
      dataItemOne: $scope.oneItems,
      dataItemTwo: $scope.twoItems ];





      share|improve this answer




























        0














        Also assign values to key like:



        $scope.data = [
        dataItemOne: $scope.oneItems,
        dataItemTwo: $scope.twoItems ];





        share|improve this answer


























          0












          0








          0







          Also assign values to key like:



          $scope.data = [
          dataItemOne: $scope.oneItems,
          dataItemTwo: $scope.twoItems ];





          share|improve this answer













          Also assign values to key like:



          $scope.data = [
          dataItemOne: $scope.oneItems,
          dataItemTwo: $scope.twoItems ];






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 6:12









          Shujaat AliShujaat Ali

          315




          315























              -1














              You have best option as @Sergey Mell given below use $q service.

              If you don't want to use $q.
              then

              Declare $scope.data = ; at top and use
              $scope.data.push($scope.oneItems)

              But make sure you have declared $scope.data to at the top otherwise it will throw error.

              your controller code will be like



                myApp.controller("myController", function($scope, $http, myFactory) {
              $scope.data = ;
              myFactory.siteOne().success(function(data, status, headers, config) {
              $scope.projects = data.d.results;
              var items = $scope.projects,
              totalItems = 0;

              for (var i=0;i<items.length;i++) {
              var currentItem = items[i];
              if(currentItem.Status!="Completed") {
              totalItems++;
              }
              };
              $scope.oneItems = totalItems;
              $scope.data.push($scope.oneItems);
              });

              myFactory.siteTwo().success(function(data, status, headers, config) {
              $scope.projects = data.d.results;
              var items = $scope.projects,
              totalItems = 0;

              for (var i=0;i<items.length;i++) {
              var currentItem = items[i];
              if(currentItem.Status!="Completed") {
              totalItems++;
              }
              };
              $scope.twoItems = totalItems;
              $scope.data.push($scope.twoItems );
              });

              /* $scope.data = [
              $scope.oneItems, $scope.twoItems
              ];*/

              console.log(JSON.stringify($scope.oneItems));
              console.log(JSON.stringify($scope.twoItems));
              console.log(JSON.stringify($scope.data));
              });





              share|improve this answer






























                -1














                You have best option as @Sergey Mell given below use $q service.

                If you don't want to use $q.
                then

                Declare $scope.data = ; at top and use
                $scope.data.push($scope.oneItems)

                But make sure you have declared $scope.data to at the top otherwise it will throw error.

                your controller code will be like



                  myApp.controller("myController", function($scope, $http, myFactory) {
                $scope.data = ;
                myFactory.siteOne().success(function(data, status, headers, config) {
                $scope.projects = data.d.results;
                var items = $scope.projects,
                totalItems = 0;

                for (var i=0;i<items.length;i++) {
                var currentItem = items[i];
                if(currentItem.Status!="Completed") {
                totalItems++;
                }
                };
                $scope.oneItems = totalItems;
                $scope.data.push($scope.oneItems);
                });

                myFactory.siteTwo().success(function(data, status, headers, config) {
                $scope.projects = data.d.results;
                var items = $scope.projects,
                totalItems = 0;

                for (var i=0;i<items.length;i++) {
                var currentItem = items[i];
                if(currentItem.Status!="Completed") {
                totalItems++;
                }
                };
                $scope.twoItems = totalItems;
                $scope.data.push($scope.twoItems );
                });

                /* $scope.data = [
                $scope.oneItems, $scope.twoItems
                ];*/

                console.log(JSON.stringify($scope.oneItems));
                console.log(JSON.stringify($scope.twoItems));
                console.log(JSON.stringify($scope.data));
                });





                share|improve this answer




























                  -1












                  -1








                  -1







                  You have best option as @Sergey Mell given below use $q service.

                  If you don't want to use $q.
                  then

                  Declare $scope.data = ; at top and use
                  $scope.data.push($scope.oneItems)

                  But make sure you have declared $scope.data to at the top otherwise it will throw error.

                  your controller code will be like



                    myApp.controller("myController", function($scope, $http, myFactory) {
                  $scope.data = ;
                  myFactory.siteOne().success(function(data, status, headers, config) {
                  $scope.projects = data.d.results;
                  var items = $scope.projects,
                  totalItems = 0;

                  for (var i=0;i<items.length;i++) {
                  var currentItem = items[i];
                  if(currentItem.Status!="Completed") {
                  totalItems++;
                  }
                  };
                  $scope.oneItems = totalItems;
                  $scope.data.push($scope.oneItems);
                  });

                  myFactory.siteTwo().success(function(data, status, headers, config) {
                  $scope.projects = data.d.results;
                  var items = $scope.projects,
                  totalItems = 0;

                  for (var i=0;i<items.length;i++) {
                  var currentItem = items[i];
                  if(currentItem.Status!="Completed") {
                  totalItems++;
                  }
                  };
                  $scope.twoItems = totalItems;
                  $scope.data.push($scope.twoItems );
                  });

                  /* $scope.data = [
                  $scope.oneItems, $scope.twoItems
                  ];*/

                  console.log(JSON.stringify($scope.oneItems));
                  console.log(JSON.stringify($scope.twoItems));
                  console.log(JSON.stringify($scope.data));
                  });





                  share|improve this answer















                  You have best option as @Sergey Mell given below use $q service.

                  If you don't want to use $q.
                  then

                  Declare $scope.data = ; at top and use
                  $scope.data.push($scope.oneItems)

                  But make sure you have declared $scope.data to at the top otherwise it will throw error.

                  your controller code will be like



                    myApp.controller("myController", function($scope, $http, myFactory) {
                  $scope.data = ;
                  myFactory.siteOne().success(function(data, status, headers, config) {
                  $scope.projects = data.d.results;
                  var items = $scope.projects,
                  totalItems = 0;

                  for (var i=0;i<items.length;i++) {
                  var currentItem = items[i];
                  if(currentItem.Status!="Completed") {
                  totalItems++;
                  }
                  };
                  $scope.oneItems = totalItems;
                  $scope.data.push($scope.oneItems);
                  });

                  myFactory.siteTwo().success(function(data, status, headers, config) {
                  $scope.projects = data.d.results;
                  var items = $scope.projects,
                  totalItems = 0;

                  for (var i=0;i<items.length;i++) {
                  var currentItem = items[i];
                  if(currentItem.Status!="Completed") {
                  totalItems++;
                  }
                  };
                  $scope.twoItems = totalItems;
                  $scope.data.push($scope.twoItems );
                  });

                  /* $scope.data = [
                  $scope.oneItems, $scope.twoItems
                  ];*/

                  console.log(JSON.stringify($scope.oneItems));
                  console.log(JSON.stringify($scope.twoItems));
                  console.log(JSON.stringify($scope.data));
                  });






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 4:36

























                  answered Jan 2 at 6:25









                  Gitaram KanawadeGitaram Kanawade

                  151314




                  151314






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001517%2fhow-to-add-a-scope-object-value-to-another-scope-object-angularjs%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas