logo
down
shadow

How to watch a variable in a Service from a Controller to open a modal?


How to watch a variable in a Service from a Controller to open a modal?

By : Tiago Borges
Date : November 22 2020, 02:42 PM
should help you out $watch also takes a function so you could have :
http://plnkr.co/edit/oJ2CTVXKCiLwL9oNw6YR?p=preview
code :
  $scope.$watch(function(){
    return AddTickerFactory.getToggleStatus();
  }, function(current, original) {
    vs.showModal = current;
      console.log('current',current);
      console.log('original',original);
  });


Share : facebook icon twitter icon
How to watch a primitive service variable in controller with AngularJS?

How to watch a primitive service variable in controller with AngularJS?


By : Igor Moreira Faria
Date : March 29 2020, 07:55 AM
seems to work fine You can pass a function that will return the value of the Service's method. Angular will then compare it to the previous value.
code :
$scope.$watch(function(){
    return authService.isLoggedIn;
}, function (newValue) {
    alert("isLoggedIn changed to " + newValue);
});
Service/Controller interaction with a $watch defined on a service property. The watch doesn't fire, can anyone clarify

Service/Controller interaction with a $watch defined on a service property. The watch doesn't fire, can anyone clarify


By : Thanh Dang
Date : March 29 2020, 07:55 AM
hop of those help? notificationService.displayedNotifications is not on the scope. You call **$scope**.$watch after all.
You can add either the service or just the property to the scope:
code :
$scope.displayedNotifications = notificationService.displayedNotifications;
$scope.watch('displayedNotifications', ...
$scope.$watch(function() {
    return notificationService.displayedNotifications.length;
  }, function (newValues, oldValues) {
angularjs watch service variable changes in controller and directive

angularjs watch service variable changes in controller and directive


By : Tymoteusz Paszun
Date : March 29 2020, 07:55 AM
Hope that helps What is the setTimeout() for? This browser's native function will not lead to dirty checking and so ng will not notice the change (until the next dirty-checking is triggered from somewhere else).
If you remove it, or replace it by $timeout (after injecting that, of course), the watch is triggered.
How to watch variable in service from controller within AngularJS application?

How to watch variable in service from controller within AngularJS application?


By : T.E
Date : March 29 2020, 07:55 AM
With these it helps Ok, I managed to find a solution for my problem. At first I want to point at that part of code in loadingService:
code :
var loadingServiceFactory = {
            setLoaderStatus: setLoaderStatus,
            getLoaderStatus: getLoaderStatus,
            isLoaderEnabled: isLoaderEnabled//<------1 line
        };

        return loadingServiceFactory;//<------2 line
   (function () {
'use strict';
angular
    .module('eventApp')
    .factory('loadingService', loadingService);

function loadingService() {

    var isLoaderEnabled = false;
    var callbacks = [];
    var loadingServiceFactory = {
        setLoaderStatus: setLoaderStatus,
        getLoaderStatus: getLoaderStatus,
        register: register
    };

    return loadingServiceFactory;

    function setLoaderStatus(numberOfRequests) {
        if (numberOfRequests === 0) {
            isLoaderEnabled = false;
        }
        if (numberOfRequests > 0) {
            isLoaderEnabled = true;
        }
        notify();
    }

    function getLoaderStatus() {
        return isLoaderEnabled;
    }

    function register(callback) {
        callbacks.push(callback);
    }

    function notify() {
        callbacks.forEach(function (callback) {
            callback();
        });
    }
}
   })();
(function () {
'use strict';
angular
    .module('eventApp')
    .controller('indexController', indexController);

indexController.$inject = ['$location', 'authService', 'authModal', 'loadingService'];

function indexController($location, authService, authModal, loadingService) {
    /* jshint validthis: true*/
    var vm = this;
    vm.loading = false;            

    vm.loaderObserver = function () {
        vm.loading = loadingService.getLoaderStatus();
    }

    loadingService.register(vm.loaderObserver);

    //other unnecessary code
};
})();
    <div id="loaderDiv" ng-show="index.loading" >
                    <img src="client/assets/img/loader.gif" class="content-loader" />
             </div>   
How $watch changes of a variable in a service from component's controller?

How $watch changes of a variable in a service from component's controller?


By : Badok
Date : March 29 2020, 07:55 AM
like below fixes the issue The problem is that you never actually change the value of vm.indexes - it always points to the same array. setIndexes only modifies properties of this array. That's why $watch, which by default checks for reference equality only, fails to spot the changes.
There are (at least) two ways of solving this: either make $watch check the object equality instead, by adding a third param there:
code :
$scope.$watch('ShareData.getIndexes()', function(newVal) {
   console.log('New indexes arrived', newVal);
}, true);
vm.setIndexes = function(firstIndexParam, lastIndexParam, message) {
   if (leaderIndexParam === undefined && partnerIndexParam === undefined) {
     return;  
   }
   vm.indexes = vm.indexes.slice();
   Object.assign(vm.indexes, {
     mainIndex: firstIndexParam, 
     secondaryIndex: lastIndexParam, 
     message: message
   });
 };
Related Posts Related Posts :
  • I want to restrict HTML input autocomplete to 3 letter minimum
  • tinyMCE editor not updating on the fly
  • typescript kendo-ui call method from string
  • HttpCompileException was unhandled by user code (when i am adding c# code in script)
  • Send form data as array of objects to controller in asp.net mvc
  • Javascript, multi file modules and Require.Js
  • how to render a complete new template and leave the application template in ember
  • Hover unrelated divs
  • Permute string until it matches some input?
  • How to fire JQuery change event when input value changed programmatically?
  • AJAX: POST data to a different url
  • checkbox column in jquery datatable
  • Document.evaluate for documents without namespaceURI crashes Microsoft Edge
  • Bootstrap Menu with "More" option Responsive issue
  • Standard inputs not being added to Angularjs Form $Scope
  • Express promise throw error
  • onclick and get in jquery
  • How to get the country code from Google Places API
  • Passing parameters url in Select2
  • Is there any frame work for Marklogic to write javascript code?
  • How to implement a boostrap template over bootstrap-sass gem
  • Why fixed div comes out when horizontally scrolled?
  • How to limit scope of a function in javascript/ jquery
  • Overlapping legend d3.js
  • Apply transition to div
  • Javascript game help - counting with dice
  • How to hide a div from angular controller
  • Can Promise load multi urls in order?
  • How enable Hover event for bootstrap menus?
  • D3 scale.invert() isn't returning date for x
  • Parsing object to array using underscore js or lodash
  • ES6 with React and Browserify
  • AngularJS Factory return type undefined
  • javascript follow html input fields
  • how to declare a double array in javascript for this particular UI
  • Jquery : find length of mixed list
  • Returning the array before the function is complete & Angular JS
  • Javascript set value of class & hidden input in nested divs
  • Register custom filters with vue.js
  • Add class to input parent
  • Angular controller not talking to view
  • Trouble creating "if then" Javascript statement to change background color of page
  • Socket.IO - Middleware and scoping: how should I access global IO object from external middleware?
  • add class when input is valid in angularjs
  • Positioning multiple div elements
  • Checkbox clicking works, deselecting checkbox not working Javascript
  • Adding ajax jQuery File upload
  • Creating a dynamic array within a dynamic array in JQuery
  • Multiple File Upload actions for a single File Input PHP/JS
  • Want to move an added element to a different part of the DOM
  • Why can't I get Angularjs to work?
  • Minimizing browser window in Firefox & Chrome adds white space to right of page
  • I need to insert the date and time of employee stamp in and out into mysql database
  • Making css auto slider
  • why can't I defne the function first?
  • Pagination with MongoDB in Node.js (With Sorting)
  • How to repeat each() without page refresh
  • Position div at position of another div
  • Click handler is called multiple times in jQuery animate
  • How to multiply each element in an array with a different number (javascript)
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co