0

我在服务中设置了一个手表(下面的代码片段),我无法弄清楚为什么它没有被触发。当对象更改时,控制器中设置的类似手表(使用 $Scope.$watch)会被触发。

我错过了什么?

(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) {
...

这段代码确实被触发了......

(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.');
...
4

1 回答 1

1

在我看来,您要查看的变量似乎不在 AngularJs 范围内,据我所知,它在这种情况下不起作用。请参阅此问题以获取解释。

您需要做的是触发一个摘要循环以使其工作。

编辑:

添加到范围应该可以解决问题:

$rootScope.db = db;
于 2015-03-14T17:29:32.543 回答