ed4becky
ed4becky

Reputation: 1630

$rootsScope.$watch not getting triggered

I have a watch set in a service (code snippet below) and I cannot figure out why it is not getting triggered. The a similar watch set in a controller (using $Scope.$watch) IS getting triggered when the object changes.

What am I missing?

(function() {
    'use strict';
    angular
        .module('evidentia')
        .service('srcPaneSvc', SourcePaneSvc);

    SourcePaneSvc.$inject = ['$rootScope', '$timeout', 'dataProvider', 'dialogSvc', 'attachSvc'];

    function SourcePaneSvc($rootScope, $timeout, dp, ds, attachSvc) {

        var svc = this;
        svc.status =
        {
            isDirty : false,
            disabled : true,
            original : null
        };

        svc.attachSrc = attachSrc;
        svc.cloneSource = cloneSource;
        svc.deleteSource = deleteSource;
        svc.dirty = dirty;
        svc.editAttachment = editAttachment;
        svc.linkOriginal = linkOriginal;
        svc.newSource = newSource;
        svc.openAttach = openAttach;
        svc.openEditor = openEditor;
        svc.saveSource = saveSource;
        svc.showTemplates = showTemplates;
        svc.srcBox = openFSSrcBox;
        svc.openSrcDetail = openSrcDetail;
        svc.unattach = unattach;
        svc.unlink = unlink;

        $rootScope.$watch('dp.source', onSourceChange, true);

        function onSourceChange() {
            if (dp.source.id) {
...

This code DOES get triggered...

(function() {
    'use strict';
    angular
        .module('evidentia')
        .controller('rightSidebarCtrl', RightSidebarCtrl);

    RightSidebarCtrl.$inject = ['$scope', '$window', '$timeout', '$location', 'dataProvider', 'syncSvc'];

    function RightSidebarCtrl($scope, $window, $timeout, $location, dp, ss) {

        var vm = this;
        vm.online = false;
        vm.auth = ss.isAvailable();
        vm.srcMode = true;
        vm.srcFilter = '';
        vm.wrapStyle = (dp.GO.wrap ? {'white-space': 'normal'} : {'white-space': 'nowrap'});

        vm.collapse = collapse;
        vm.toggleSrc = toggleSrc;
        vm.subClick = subClick;
        vm.srcClick = srcClick;
        vm.refClick = refClick;

        $scope.$watch('dp.source', function () {
            _ev.dbg('RightSiderBarCtrl: source changed.');
...

Upvotes: 0

Views: 103

Answers (1)

Sjoerd222888
Sjoerd222888

Reputation: 3476

To me it looks as if the variable you want to watch is not in an AngularJs scope and as far as I know it will not work in this case. See this question for an explanation.

What you need to do is trigger a digest cycle to get it to work.

EDIT:

Adding to the scope should solve the problem:

$rootScope.db = db;

Upvotes: 1

Related Questions