3

I want add new array in existing array dynamically. Here I am try to add array to the existing array in a shared service.

here is my code

var shareService = angular.module('ibaDataService', []);
shareService.service('shareData', function () {
    this.comonData = [];
    this.tabData = [];
    this.pushArray = function (arrayName) {
        //this.comonData.push(arrayName[]);
          // here i want to add commonData.newArray[]
 }
    });
0

5 Answers 5

1

You need to store the value of this in a variable before referencing it within a nested function.

For example:

var shareService = angular.module('ibaDataService', []);
shareService.service('shareData', function () {
    var self = this;
    this.comonData = {};
    this.tabData = [];
    this.pushArray = function (key, value) {
        // self holds the value of the parent instance
        self.comonData[key] = value;

}});

self is now being used to maintain a reference to the original this even as the context has changed (inside another function).

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

7 Comments

thank you,its shows some syntax error on arrayName[]
right, remove the [] so it becomes self.comonData.push(arrayName);
@Ajith an array can only be indexed by an integer value, so if you want to get the first value in the array you'd call self.comonData[0] - if you need an to store and retrieve a key/value combination, the array is not going to work. You need to use a javascript object which is {}
@Ajith - updated the answer. Now, when you call pushArray you need to pass 2 arguments. for example: pushArray('new', true); - Hope this is clear
@Ajith I believed I answered your question before and so my answer should be accepted as correct and I suggest you ask another question about using an object instead of an array in this example. People who come to this question in the future should not feel confused about the resolution if the subject is changing.
|
1

Use Array#concat:

this.comonData = this.comonData.concat(arrayName);

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

Comments

1

use array concat

var shareService = angular.module('ibaDataService', []);
shareService.service('shareData', function () {
    this.comonData = [];
    this.tabData = [];
    this.pushArray = function (arrayName) {
        this.comonData = this.comonData.concat(arrayName);
  }
});

1 Comment

thank you azzi, this includes ["arrayName"], i want to access the new array like this.commonData.newArray
1

As you want to do it dynamically, I think you could use a "watch" :

$rootScope.$watch('variableToWatch', function() {
       // - Call concat code here
   });

Your code is then executed each time a source data is changed. Of course you have to add "$rootScope" dependency to your service.

2 Comments

Elo, can u paste a example here?
I think the concat method itself is well described by other answers. The final code depends on the way you receive the data you want to add to the array (Ajax call ?). And moreover I am not sure I had correctly understood your final need, but as soon as you mentionned "dynamically", I thought observers with $watch could help.
0

You can merge an array using concat:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
// Result is: Cecilie,Lone,Emil,Tobias,Linus

In your case:

this.comonData.concat(someOtherArray);

Comments

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.