jquery获取功能的价值

时间:2017-08-24 06:55:30

标签: jquery

export class SongsService {
        albumsChanged = new Subject();

        //Define future album name and songs list
           newAlbumName:any;
           newSongs:any;
           newAlbumID:any;

        // New album object    
           albumData:{name: string, songs:Array<string>};

           albums = [
            {
                name: 'One album',
                songs: [
                    'song1 Album 1',
                    'song2 Album 1',
                    'song3 Album 1',
                ]
        }];

        albumSongs(albumID){
           return this.http.get('http://api.musicgraph.com/api/v2/album/'+
           albumID +'/tracks?"+ api_key + "&limit=30").map(
              (res:Response) => {   
           const songData = res.json();     

        // func to get all tracks
        var tracks = songData.data.map(a => a.title);
        // Function to filter repeated songs
        var uTracks = tracks.filter(function(elem, index, self) {
          return index == self.indexOf(elem);
        });
        return this.allTracks= uTracks;
       }
      );
    }

   // .subscribe(album => {console.log(album)}) returns []
   newAlbum(name:string){
   name = name.replace(/ /g, '+');
   var albumID= '';
   this.http.
   get('http://api.musicgraph.com/api/v2/album/suggest?'
   api_key + '&limit=2&prefix=' + name)  
   .subscribe(      
   ( res:Response) => { 
        var rawData = res.json();       
        // console.log(rawData);
        var albumName:string = rawData.data[0].title;
        albumID = rawData.data[0].id;

        this.albumSongs(albumID).subscribe(

            songs => {
            this.newSongs = songs;
            console.log("And this are the songs in newAlbum = " + this.newSongs );
            this.albumData = {
                name:albumName,
                songs:this.newSongs
            };
            this.albums.push(this.albumData)
            this.albumsChanged.next(this.albums.slice())
            }
        );
    }
  );
  this.albumsChanged.next(this.albums.slice());
}

        newAlbum (name:string) {
       //function that gets album name from api
          this.albums.push(albumObj);
          this.albumsChanged.next(this.albums.slice());
        }

我想让uservar值无法使用。

1 个答案:

答案 0 :(得分:0)

var uservar; // it makes uservar globally defined
$(".newadded").click(function () {
var array = $('#usernames').val().split(",");
    $.each(array,function(i)
    {
        var a = (array[i]);
        if(a !="")
        {
            $('#accordian .custSecCont').show();
            uservar += "<option value='" + array[i]  + "'>" + array[i] + "</option>";  
        }
    });
});