TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Niels Dek
NA
7
565
Call a function inside a directive from controller
Aug 7 2019 3:16 PM
Hi there people of c-sharpcorner,
I got a player that displays the currently or latst played song. Now in order to keep the content updated. I used $interval and $scope.$watch to register the playing song, which displays in the developer console.
Now here's the point I've been stuck on for a few days, since my knowledge of Angular is very limited.
Everytime a change in the songname is detected I want to fire the variable "link" which fires the load() function (this is essentially the one that needs to fire). However, the varbiale is in the controller module. How do I call it?
Like I said, I'm not familiar enough with AngularJS to really edit the code, but I managed to get updated song info, alang with an if-statement so it only fires when a change is detected between the two funtions CurrentSong and LatestSong
angular.module(
'app'
, [
'lastfm-nowplaying'
])
.controller(
'mainCtrl'
, [
'$scope'
,
'$http'
,
'$interval'
,
function
($scope, $http, $interval) {
$scope.lastFmConfig = {
apiKey:
'API'
,
user:
'USER'
,
containerClass:
'content'
};
$scope.$watch(
'currentsongs'
,
function
(CurrentSong, LatestSong) {
if
(CurrentSong != LatestSong) {
console.log(
"Change detected"
);
console.log($scope.songdata);
}
},
true
);
var
checkData =
function
(songcheck) {
$http({
method:
'GET'
,
url:
'https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=USER&api_key=API&format=json&limit=1'
}).then(
function
(songdata) {
$scope.songdata = songdata;
$scope.currentsongs = songdata.data.recenttracks.track[0].name;
console.log($scope.currentsongs);
},
function
(error) {
//oopsie
});
};
checkData();
$interval(
function
() {
checkData();
}, 10000);
}]);
angular.module(
'lastfm-nowplaying'
, [])
.directive(
'lastfmnowplaying'
, [
'uiCreation'
,
'lastFmAPI'
,
'lastFmParser'
,
function
(uiCreation, lastFmAPI, lastFmParser) {
var
link =
function
(scope, element, attrs) {
scope.$watch(
'config'
,
function
(value) {
load();
});
var
load =
function
() {
var
latestTrack;
//irrelevant-code
};
return
{
scope: {
config:
'=config'
},
link: link
};
}])
I really hate to ask, but I'm really stuck here. If someone could help me out? I feel like the solution is pretty simple.
Reply
Answers (
2
)
visual studio code (MyCode) in disappear
Decorator authorization permission in angular ?