Reputation: 4425
I have a global variable which is used to get information from server each 3 minutes if there are new messages. The variable name is HasNewMessage. Now what I want to do is to get the value of that variable in one of my AngularJS apps.
My directive is this
commonApp.directive('osMessageList', ['getMessages', '$location', '$anchorScroll', 'searchMessages', 'userfactory', '_', '$rootScope', '$window',
function (getMessages, $location, $anchorScroll, searchMessages, userfactory, _, $rootScope, $window) {
return {
restrict: 'A',
scope: {
messages: "="
},
controller: function ($scope) {
$scope.hasNewMessages = false;
$scope.$watch(function () {
return $scope.hasNewMessages;
}, function (newValue, oldValue) {
if (newValue) {
}
}, true);
},
function startInterval() {
// do something
// get scope of that directive and update the value
},SOME_TIME)
How can I acheive this?
Upvotes: 0
Views: 45
Reputation: 594
If the startInterval function is outside the angularjs space (ie. global) it's a bit trickier. I assume there's a reason you can't simply use the $interval service and keep it all in the angularjs framework.
You could try using a native js event, and add a listener in your angularjs controller with reference to the services/scopes/etc in the callback of the listener.
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
Create the event globally on page load or something. Dispatch it in your startInterval callback when the HasNewMessage state changes.
Add the listener in your directive controller, similar to the watch.
Upvotes: 1