如何使用controllerAs语法实时更新另一个angular控制器

How to update another angular controller in real-time using controllerAs syntax

本文关键字:另一个 angular 控制器 实时更新 语法 何使用 controllerAs      更新时间:2023-09-26

我目前正在尝试使用一个控制器的服务从web API检索行计数值,然后在从DB检索值后更新不同的控制器变量-同时避免使用$scope或$rootScope。

下面是Controller1——当服务中的值发生变化时,这个控制器需要更新它的变量。目前它运行正常,但我想避免使用$scope或$rootScope:

(function() {
'use strict';
angular
    .module('app.event')
    .controller('Controller1', Controller1);
Controller1.$inject = ['service1', '$stateParams', '$rootScope'];
/**
 * Controller 1
 * @constructor
 */
function Controller1(service1, $stateParams, $rootScope) {
    // Declare self and variables
    var vm = this;
    vm.number = 0;
    init();
    $rootScope.$on('countChanged', refresh);
    /**
     * Initializes the controller
     */
    function init() {
        service1.refreshCount($stateParams.id);
    }
    /**
     * Refreshes the count
     * @param {object} event - The event returned from the broadcast
     * @param {int} count - The new count to update to
     */
    function refresh(event, count) {
        vm.number = count;
    }
}
})();

服务-我想避免使用$rootScope。美元广播:

(function() {
'use strict';
angular
    .module('app.event')
    .factory('service1', service1);
service1.$inject = ['APP_URLS', '$http', '$rootScope'];
/**
 * The service
 * @constructor
 */
function service1(APP_URLS, $http, $rootScope) {
    // Declare
    var count = 0;
    // Create the service object with functions in it
    var service = {
        getCount: getCount,
        setCount: setCount,
        refreshCount: refreshCount
    };
    return service;
    ///////////////
    // Functions //
    ///////////////
    /**
     * Re-calls the web API and updates the count
     * @param {Guid} id - The ID needed for the API call parameter
     */
    function refreshCount(id) {
        $http({ url: APP_URLS.api + '<TheAPINameHere>/' + id, method: 'GET' }).then(function (response) {
            setCount(response.data.Count);
            changed();
        });
    }
    /**
     * Returns the count value
     */
    function getCount() {
        return count;
    }
    /**
     * Re-calls the web API and updates the count
     * @param {int} newCount - The new count value
     */
    function setCount(newCount) {
        count = newCount;
        changed();
    }
    /**
     * Broadcasts a change event to be picked up on in Controller1
     */
    function changed() {
        $rootScope.$broadcast('countChanged', count);
    }
}
})();

下面是Controller2中的一个函数,用于更新服务中的值-我希望能够在我这样做时立即获得Controller1中的值:

/**
 * Removes a row from the database
 * @param {object} field - The data row object that we're deleting from the table
 */
function remove(field) {
    // Delete the row from the database
    service2.delete(field.Id);
    // Remove the row from the local data array and refresh the grid
    vm.data.splice(vm.data.indexOf(field), 1);
    // Set the count in the service to update elsewhere
    service1.setCount(vm.data.length);
    vm.indices.reload();
}

我避免使用$rootScope用于实践目的,但它似乎是最好的方法。当我试图根据其他建议使用公共属性时,它不适合我,所以我使用了$rootScope。