【问题标题】:How to refresh div in angularjs如何在angularjs中刷新div
【发布时间】:2016-11-18 12:52:18
【问题描述】:

我在 angularjs 应用程序文件中进行了计算,基本上每次更改 HTMl 表单中的值时,计算结果都会反映在 span 块reloader_block 中,

我想要做的是我想添加动画,以便整个跨度块应该闪烁并且用户应该注意到它。

例如,span 块应该在几毫秒内淡出和淡入。

我该怎么做?

$scope.refreshDiv = function () {
  //here i want to refresh the span block `reloader_block`   
};

HTML:

<span class="block reloader_block">
  Here is how you can Walden this product: 
  You will have to share it with <B>{{sharable_with}}</b> users. Each one of you  will have to give us  <b>{{walden_costs}}</b>/- 
  Each of you will get to use the product for <b>{{usage}}</b> days after every <b>{{cycle}}</b> days
  after <b>{{product_warranty}}</b> year(s) we will auction the product at <b>30%</b> of MRP.
  {{priceful}}
</span>

【问题讨论】:

  • 在一个简单的 $watch 中调用 refreshDiv 怎么样?

标签: html angularjs


【解决方案1】:

您只需在块上删除并添加一个带有 ng-class="flashback" 的类,然后在您的函数中清除变量并使用动画背景 css 再次设置它。在此处找到类似的 css 解决方案 (A "flash" of color, using pure css transitions)

$scope.refreshDiv = function () {
    $scope.flashback = "";
    $scope.flashback = "demo";
};

HTML:

<span ng-class="flashback"></div>

css:

@-webkit-keyframes demo {
    0% {
        background-color: Yellow;
        opacity:1;
    }

    100% {
        background-color: #777;
    }
}

.demo {
    -webkit-animation-name: demo;
    -webkit-animation-duration: 900ms;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-timing-function: ease-in-out;
}    

【讨论】:

    【解决方案2】:

    您可以结合使用 css 过渡和$timeout

    .reloader_block {
        transition: opacity 1s ease-in;
    }
    
    .reloader_block-active {
        opacity: 0.5;
    }
    
    $scope.refreshDiv = function() {
        var reload_element = document.getElementsByClassName('reloader_block')[0];
        reloader_element.classList.add('reloader_block-active');
        $timeout(function() {
            reloader_element.classList.remove('reloader_block-active');
        }, 500);
    }
    

    【讨论】:

    • document.getElementsByClassNames 不是有效的 Angular 代码
    • 它是有效的 javascript 代码。为什么选择元素需要角度?
    • 好的,classList.add 也是无效的,因为您正在修改 DOM 而 Angular 不知道它,它可能会起作用,但不是 Angular 的做事方式
    • angular 不需要知道修改 DOM。它只需要知道何时使用某种双向数据绑定。
    • Angular 也是关于在没有此类代码的情况下操作 DOM。当你手中有 Angular 时,它是有效的 JS,但不是一个好习惯。
    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2015-03-17
    • 2014-04-23
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多