2015-10-20 74 views
2

我是angularJS的新手,我在下面的代碼中遇到了一些問題。基本上,我試圖實現的目標是搜索Spotify API中的詞彙並檢索給予我的第一個播放列表,然後抓取播放列表的URI並將其連接到要在頁面中顯示的嵌入網址。將Javascript對象轉換爲Angular JS中的字符串

我似乎無法得到$ scope.spotifySearch對象以字符串格式進入$ scope.playlisturi範圍。任何幫助,將不勝感激。

app.controller('MainCtrl', function ($scope, $sce, Spotify) { 

    $scope.spotifySearch = Spotify.search('Top 40', 'playlist').then(function (data) { 
    return (data.playlists.items[0].uri); 
    }); 

    $scope.trustSrc = function(src) { 
    return $sce.trustAsResourceUrl(src); 
    } 

    $scope.playlisturlfragment = "https://embed.spotify.com/?uri="; 
    $scope.playlisturi = $scope.spotifySearch; 
    $scope.playlisturl = {src:$scope.playlisturlfragment+$scope.playlisturi} 

}); 
+0

FYI:我使用搜索Spotify的API庫是角Spotify的 –

+0

這個問題意味着你想JSON但我認爲你想要的是URL查詢參數。你可以在你的'.then(..'函數回調函數中使用 – cgTag

+0

。「#scope-playlisturi' – devqon

回答

2

您正在分配承諾spotifySearch,我想你想要的是通過搜索返回的uri

基本上有兩種解決這個問題的方法。

  1. 解決承諾時將值賦給playlisturl
  2. spotifySearch更改時,將值指定爲playlisturl

這是第一種方法。

$scope.playlisturlfragment = "https://embed.spotify.com/?uri="; 
Spotify.search('Top 40', 'playlist').then(function (data) { 
    $scope.playlisturl = {src:$scope.playlisturlfragment+data.playlists.items[0].uri} 
    }); 

另一種方法是觀察spotifySearch的變化。

$scope.playlisturlfragment = "https://embed.spotify.com/?uri="; 
Spotify.search('Top 40', 'playlist').then(function (data) { 
    $scope.spotifySearch = data.playlists.items[0].uri; 
    }); 

$scope.$watch('spotifySearch',function(value) { 
    if(!value) return; 
    $scope.playlisturl = {src:$scope.playlisturlfragment+value}; 
}); 
+0

爲什麼添加手錶,而不是隻是在回調中更新'$ scope.playlisturl'?也可能需要'$ apply' – charlietfl

+0

第一種方法完美地工作,感謝您的幫助,我意識到我現在要去的地方錯了:) –

+0

我會選擇第一個 – devqon

1

你分配的承諾你$scope.spotifySearch,而不是真正的數據($http返回一個承諾)。相反,你可以這樣做:

app.controller('MainCtrl', function ($scope, $sce, Spotify) { 

    $scope.playlisturlfragment = "https://embed.spotify.com/?uri="; 
    $scope.playlisturl = { 
     src: "" 
    } 

    $scope.spotifySearch = Spotify.search('Top 40', 'playlist').then(function (data) { 
     // use the callback to assign the uri to your object 
     $scope.playlisturl.src = $scope.playlisturlfragment + data.playlists.items[0].uri; 
    }); 

}); 
0

角度使用承諾的請求。這個承諾是異步的,很快返回值然後不起作用。用於這種用途的回調函數爲:

app.controller('MainCtrl', function ($scope, $sce, Spotify) { 

    $scope.trustSrc = function(src) { 
     return $sce.trustAsResourceUrl(src); 
    }; 

    $scope.init = function(){ 
     $scope.search(function(uri){ 
      $scope.playlisturlfragment = "https://embed.spotify.com/?uri="; 
      $scope.playlisturl = {src:$scope.playlisturlfragment + uri}; 
     }); 
    }; 

    $scope.search = function(callback){ 
     $scope.spotifySearch = Spotify.search('Top 40', 'playlist').then(function (data) { 
      callback(data.playlists.items[0].uri); 
     }); 
    }; 
}); 

運行的外觀init方法這個例子

+0

將承諾轉換爲回調對我來說是一種螞蟻模式。它隱藏了承諾的好處。 – cgTag

+0

如何使用角度異步任務? –

+0

我看你的例子。您使用$ watch等待承諾的回報。對我來說這是螞蟻模式。在開發中使用$ watch大項目可能會損害代碼,如果開發在代碼的其他部分更改模型。回調函數保證了app中的獨特功能。在這種情況下,你需要兩個補充。你做了什麼?爲$觀看這個創建其他變量? –

相關問題