从具有参数的角控制器启动函数



我需要启动一个需要从我获得的json中参数的函数,请求是:

app.controller('getDataCtrl', function ($scope, $http, $ionicLoading) {
$http({
    method: "POST",
    url: "http://someurl/GetPlaylist",
    dataType: "json",
    contentType: "application/json; charset=utf-8"
}).then(function mySucces(response) {
    $scope.allTracks = response.data;
    $scope.recentTracks = response.data.Tracks;
//now i want to luanch the function below that written in a different js file
//and pass parameters
        **startCration(recentTracks[0].Song, recentTracks[0].Artist);**
}, function myError(response) {
    $scope.allTracks = response.statusText;
});
});

这是我需要启动的功能

function startCration(song, artist) {
    MusicControls.create({
        track: song,        
        artist: artist,                           
    }, onCreateSuccess, onCreateError);

,但我似乎无法在成功之后调用该功能

当您将所有轨道分配给$scope.recentTracks时,您应该参考$scope.recentTracks而不是recentTracks

startCration(recentTracks[0].Song, recentTracks[0].Artist);

应该是

startCration($scope.recentTracks[0].Song, $scope.recentTracks[0].Artist);

您基本上可以广播成功的事件。这是更新的代码:

app.controller('getDataCtrl', function ($scope, $http, $ionicLoading,$rootScope) {
$http({
    method: "POST",
    url: "http://someurl/GetPlaylist",
    dataType: "json",
    contentType: "application/json; charset=utf-8"
}).then(function mySucces(response) {
    $scope.allTracks = response.data;
    $scope.recentTracks = response.data.Tracks;
//now i want to luanch the function below that written in a different js file
//and pass parameters
 $rootScope.$broadcast('response-recieved', { response: response.data });
}, function myError(response) {
    $scope.allTracks = response.statusText;
});
});

现在,在您的不同JS文件中,您可以按以下方式收听上述事件:

$scope.$on('response-recieved', function(event, args) {
    var res= args.response.Tracks;
    // do what you want to do
    startCration(res[0].Song, res[0].Artist); // call to your function.
});

最新更新