1

現在、あるコントローラーのサービスを使用して 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.$broadcast の使用は避けたい:

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

1 に答える 1

1

練習目的で $rootScope を使用することは避けていましたが、これが最善の方法のようです。他のいくつかの提案に従ってパブリック プロパティを使用しようとしたときに、パブリック プロパティが機能しなかったため、CainBot が提案したように $rootScope.$emit を使用しました。

于 2015-09-25T00:25:49.483 に答える