【问题标题】:how to update scope value after 5 sec in constructor typescript如何在构造函数打字稿中 5 秒后更新范围值
【发布时间】:2016-09-16 06:19:45
【问题描述】:

我面临一个问题,我必须在 5 秒后刷新范围值。这是我的控制器构造函数,我在其中初始化值并调用超时函数以在 5 秒后刷新内容。

static $inject = ['$scope', 'selectedCardAccount', 'CardService', 'ecAnimationService', '$interval', '$modal','$rootScope','$timeout','PromiseTimeout','$q','$stateParams'];
            constructor(public $scope,public selectedCardAccount, public CardService:services.CardService, public $interval:ng.IIntervalService, public $modal,public $rootScope:ng.IRootScopeService, public $timeout,public PromiseTimeout,public $q,public TrackPendingRequest,public $stateParams) {

    //setting the current value
    this.account = selectedCardAccount.account;

    //calling a serive again after five section to referesh the content
    $timeout(function(){
            //delete the current object
        delete this.account;
        //calling service to get result
                CardService.getAccountAndCardByShortName($stateParams.productShortName,$stateParams.id).then((succRess)=>{

            //setting the value
            //Error : cannot read property of account
            this.account = succRess.account;
        });
    }, 5000);
}

错误:无法读取帐户属性

指令代码:

module ec.directives {
        export class easein{
            /**
             * A directive is not new:ed up so we don´t specify anything on this class
             * */
            constructor(){
                var directive: ng.IDirective = {};
                directive.link = (scope:IEaseInNumberScope, element, attrs, ctrl:controllers.EaseInNumberCtrl) => {
                    ctrl.animateRemainingValue(scope);
                };
                directive.restrict = "EA";
                directive.template = "{{current | number : 2}}";
                directive.controller = 'EaseInNumberCtrl';
                directive.scope = {
                    duration: "=?",
                    startValue: "=?",
                    endValue: "="
                };

                return directive;
            }
        }
}

HTML:

<div class="row">
    <div class="col-xs-6">
        <h2 class="heading-small-bold light mar-l-1"
            ng-bind="'used_amount' | i18n"></h2>

        <h1 class="heading-big-thin-medium mar-l-1">
            <easein end-value="vm.account.usedCredit"></easein>
        </h1>
    </div>
    <div class="col-xs-6 align-r">
        <h2 class="heading-small-bold light mar-r-1"
            ng-bind="'left_to_spend' | i18n"></h2>

        <h1 class="heading-big-thin-medium mar-r-1">
            <easein start-value="vm.account.creditLimit"
                    end-value="vm.account.openToBuy"></easein>
        </h1>
    </div>
    <div class="col-xs-12 mar-t-2">
        <progressbar class="progress-bar" value="vm.loadingValue"
                     max="vm.account.creditLimit"
                     type="warning"></progressbar>
        <div class="flexible-header">
            <h2 class="heading-small-bold light"
                ng-bind="'last_transactions' | i18n"></h2>
        </div>
    </div>
</div>

如果有人建议我如何在构造函数中刷新 5 部分之后的值,请帮助。

【问题讨论】:

  • 这是您得到的真正错误吗?通常你会得到cannot read property propName of whatever。你能把你得到这个错误的行号放在哪里吗?

标签: javascript angularjs typescript constructor


【解决方案1】:

根据错误消息,帐户在 succRess 上不可用。

您应该尝试查看 succRess 是否为有效的 JSON 对象,如果服务按原样从服务器返回数据,则需要使用 JSON.parse 将其反序列化为 JSON 对象。

此外,您可能需要考虑使用 $timeout,以便该属性的侦听器可以感知值的变化。

【讨论】:

  • succRess 正在返回正确的对象。我只需要将其分配给 this.account
  • 你看到 succRess 对象上的 account 属性了吗?
【解决方案2】:

我认为您的方法存在错误。

首先您需要在 Typescript 中使用 lambda(箭头函数),以便在 $timeout 回调中正确设置构造函数的 this 上下文。

第二,没有理由使用delete关键字。使用新结果设置this.account的值,同时保持初始对象引用不变。

static $inject = ['$scope', 'selectedCardAccount', 'CardService', 'ecAnimationService', '$interval', '$modal','$rootScope','$timeout','PromiseTimeout','$q','$stateParams'];
constructor(public $scope,public selectedCardAccount, public CardService:services.CardService, public $interval:ng.IIntervalService, public $modal,public $rootScope:ng.IRootScopeService, public $timeout,public PromiseTimeout,public $q,public TrackPendingRequest,public $stateParams) {

    //setting the current value
    this.account = selectedCardAccount.account;

    //calling a serive again after five section to referesh the content
    $timeout(() => {

         CardService
             .getAccountAndCardByShortName($stateParams.productShortName,$stateParams.id)
             .then((succRess)=>{
                  angular.copy(succRess.account, this.account);
              });
    }, 5000);
}

【讨论】:

  • 现在 this.account 得到更新,但视图中的值没有改变。我有自定义指令来显示这个值
  • 那是因为对初始对象的引用发生了变化。 this.account 的类型是什么?保留初始引用并更新重要的属性。
  • 那么我必须做什么来更新自定义指令的值
  • 我更新了我的答案。尝试使用angular.copy(succRess.account, this.account);
  • 不是它不起作用,我也尝试运行 $apply 来更新值,但没有任何内容得到 chqnaged
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2017-11-25
  • 2016-09-14
  • 2022-01-23
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多