簡體   English   中英

Angular指令模板CSS類更改延遲

[英]Angular directive template css class change delayed

我的角度指令類的代碼片段:

if (currentIndex < newMessages.length) {
  scope.message = newMessages[currentIndex];
} else {
  scope.message = newMessages[0];
}                        

$timeout(function () {
  var msgText = $('#msg-text');
  console.log("xx:" + msgText.attr('class'));
  // has-image no-desc no-desc-remove
});

當我更改scope.message時,angular會刷新模板,並且$ timeout確保在使用新元素等更新模板后調用其包含功能。

看來它正在運行,但是問題是,我得到了類似“具有圖像的無desc的no-desc的刪除 ”的類。 后來將其更改為“具有圖像”。

在班級已完全更新的情況下,我該如何捕捉和事件? 因為這對我的代碼邏輯沒有好處。

在班級已完全更新的情況下,我該如何捕捉和事件? 因為這對我的代碼邏輯沒有好處。

根據您的代碼和要求,您需要知道$scope.message.imageURL$scope.message.Description更新的時刻。 對於最基本的方法,您可以使用$scope.$watchGroup()

$ watchGroup的示例。 (這里我把兩個條件都具有一些值,您可以按照自己想要的方式調整條件)

$scope.$watchGroup(['message.imageURL', 'message.Description'], function (newValues, oldValues){

  // newValues[0] => is => $scope.message.imageURL
  // newValues[1] => is => $scope.message.Description

  // case of both having some value
  if(newValues[0] &&  newValues[1]){
    console.log('things has been changed');
  }
});

原來我的html中包含angular-animate.js,它會自動執行“ Class和ngClass動畫掛鈎”。 更多內容請參見https://docs.angularjs.org/guide/animations 使用$ animate.enabled(false,myElement);禁用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM