Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to implement a way for my program to keep track of filters the user has selected and searched through. The input will be in a text-box, multiple text boxes. I have a textbox for each option (TimeStamp, SourceHost, UUID, etc) [I'm building an internal application]. I need to be able to carry the filter list for my parameters and I decided to use an Array object but I am having some issues. I initialize my array but cannot access the information in other functions

app.controller("AppCtrl", function($http, $scope){
    var app= this;
    $scope.filterList = [];
    $scope.filterList.push("test");
    console.log($scope.filterList);
    $scope.incData = function(page, filterList){
        callingIrrelevantFunction(page)
        console.log($scope.filterList);

The first console.log outputs "test". But when I use it later on (this isn't my intended use case I just wanted to make sure I could scope an array and call it inside of function) the output isn't correct. The actual intended use case is that I will be appending it to the end of the url.

Edit: Here is a shareable version of my code:

app.controller("AppCtrl", function($http, $scope){
   var app = this;
   $scope.filterList = [];
   $scope.filterList.push("uuid=9022");
   $scope.filterList.push("source_host=Kani");

   function foobar(filterList){
       console.log("inside foobar");
       console.log(filterList);
   }
   foobar($scope.filterList);
});

Outputs "[]".

Edit #2:

When change my code to just an array, without "$scope." in front of it, the code works. But I believe I do need the "$scope." for it to work with angular directives.

share|improve this question
    
from your code. i can't see why your array wouldn't have the same content. you may have other functions tempering the content. part of the code that is not displayed here. –  Dayan Moreno Leon Jun 19 at 13:48
    
Echoing @DayanMorenoLeon, can you post your entire controller? –  David Souther Jun 19 at 14:05
    
What changes callingIrrelevantFunction(page) made to $scope.filterList?. –  Krzysztof Safjanowski Jun 19 at 14:09
    
Why are you assigning strings to $scope.filterList.push in your 'shareable' version? –  Tacoman667 Jun 19 at 14:33
    
@Tacoman667 I believe that was my error and I over looked it, thank you for pointing that out. I believe everything is working now. –  Sleep Deprived Bulbasaur Jun 19 at 14:45

1 Answer 1

up vote 0 down vote accepted

The problem is that you are assigning string values to $scope.filterList.push in your 'shareable' version. You are never actually pushing the values into the array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.