Run function from angular controller with parameters
I need to run a function that needs parameters from json that I receive, request:
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;
});
});
and here I need to run the function
function startCration(song, artist) {
MusicControls.create({
track: song,
artist: artist,
}, onCreateSuccess, onCreateError);
but i can't call the function after success
+3
source to share
2 answers
You can basically broadcast a success event. Here's the updated code:
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;
});
});
Now in your other js file, you can listen to the above event like this:
$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.
});
+1
source to share