javascript - 如何使用controllerAs语法实时更新另一个 Angular Controller

标签 javascript angularjs controlleras

我目前正在尝试使用一个 Controller 的服务从 Web API 检索行计数值,然后在从数据库检索该值后更新不同 Controller 的变量 - 同时避免使用 $scope 或 $根范围。

下面是Controller1 - 当服务中的值发生变化时,该 Controller 需要更新其变量。目前它运行正常,但我想避免使用 $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();
}

最佳答案

我避免使用 $rootScope 进行练习,但这似乎是最好的方法。当我尝试根据其他一些建议使用公共(public)属性(property)时,公共(public)属性(property)对我不起作用,因此我按照 CainBot 建议使用 $rootScope.$emit 。

关于javascript - 如何使用controllerAs语法实时更新另一个 Angular Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772107/

相关文章:

angularjs - 在单页应用程序中使用 $rootScope 的 ngHide/ngShow

Javascript:获取动态创建的div的innerHTML

javascript - 使用 Active Directory(带 LDAP)在前端的 angularjs/javascript 上进行身份验证 - 流程流程应该是什么?

javascript - 未捕获的类型错误 : this. props.onDelete 不是函数 reactjs

javascript - Ng-Switch 破坏 <select> 默认/Ng-Init 值

directive - 如何从指令 Controller 访问 ngModel

javascript - 使用 AngularJS 中的表单对 ControllerAs 进行单元测试

angularjs - ngrepeat 别名为和 Controller 为

javascript - 更改类名但不反射(reflect)结果

javascript - 当我单击菜单时,子菜单部分不可见?