Angular JS - 如何在模型更改时制作动画?

2024-05-17

当 currentVertical 改变时,我试图做一个很好的淡出+淡入过渡。 在淘汰赛中,这很简单,但我在这里无法弄清楚。请帮忙。

以下代码显示一个 UL 列表,当单击 LI 元素时,该列表“绑定”到 $scope.currentVertical 中的定价数组,$scope.currentVertical 会发生更改,并且 UL 列表也会相应更新。这工作正常,但我希望整个 #container div 在 $scope.currentVertical 更改时淡出和淡入。请帮忙...

My html:

<body>
    <h1>Pricing Poll</h1>
    <div ng-controller="VerticalsController">
        <div id="container">
            <h2>{{currentVertical.title}}</h2>

            <ul>
                <li ng-repeat="pricing in currentVertical.pricings">
                    <a ng-click="currentVertical.selectPricing(pricing)">{{pricing.name}}</a>
                </li>
            </ul>
        </div>
    </div>
</body>

我的 JavaScript:

function VerticalsController($scope) {

  $scope.verticals = [
    {
        title:'internet',
        pricings: [
            {
                name: 'netvision',
                monthly: 23
            },
            {
                name: 'hot',
                monthly: 33
            },
            {
                name: '012',
                monthly: 28
            }
        ]
    },
    {
        title:'cellular', 
        pricings: [
            {
                name: 'cellcom',
                monthly: 20
            },
            {
                name: 'pelephone',
                monthly: 30
            },
            {
                name: 'orange',
                monthly: 25
            }
        ]
    },
    {
        title:'banks', 
        pricings: [
            {
                name: 'leumi',
                monthly: 20
            },
            {
                name: 'poalim',
                monthly: 30
            },
            {
                name: 'benleumi',
                monthly: 25
            }
        ]
    }];

    $scope.selected = [
    ];

    $scope.currentIndex = 0;
    $scope.currentVertical = $scope.verticals[0];

  $scope.selectPricing = function(pricing) {
    $scope.selected.push(pricing);
    $scope.currentIndex++;
    $scope.currentVertical = $scope.verticals[$scope.currentIndex];
  };

  /*$scope.remaining = function() {
    var count = 0;
    angular.forEach($scope.todos, function(todo) {
      count += todo.done ? 0 : 1;
    });
    return count;
  };*/
}

您必须使用自定义或创建指令来启动高级 DOM 操作(例如动画)。

这是您要求的动画的小提琴,我使用visible作用域上的变量触发淡入淡出,$timeout 服务仅在淡出时更改 selectedItem,可以改进以传递超时和回调作为指令选项...

Fiddle: http://jsfiddle.net/g/Bs66R/1/ http://jsfiddle.net/g/Bs66R/1/

JS:

function VerticalsController($scope, $timeout) {

  $scope.verticals = [{
    title:'internet',
    pricings: [{
      name: 'netvision',
      monthly: 23
    },
    {
      name: 'hot',
      monthly: 33
    },
    {
      name: '012',
      monthly: 28
    }]
  },
  {
    title:'cellular',
    pricings: [{
      name: 'cellcom',
      monthly: 20
    },
    {
      name: 'pelephone',
      monthly: 30
    },
    {
      name: 'orange',
      monthly: 25
    }]
  },
  {
    title:'banks',
    pricings: [{
      name: 'leumi',
      monthly: 20
    },
    {
      name: 'poalim',
      monthly: 30
    },
    {
      name: 'benleumi',
      monthly: 25
    }]
  }];

  $scope.selected = [
  ];

  $scope.currentIndex = 0;
  $scope.currentVertical = $scope.verticals[0];

  $scope.selectPricing = function(pricing) {
    $scope.selected.push(pricing);
    $scope.currentIndex++;
    $scope.visible = false;

    $timeout(function(){
      $scope.currentVertical = $scope.verticals[$scope.currentIndex];
      $scope.visible = true;
    }, 1000);
  };

  $scope.visible = true;
}

var fadeToggleDirective = function() {
  return {
    link: function(scope, element, attrs) {
      scope.$watch(attrs.uiFadeToggle, function(val, oldVal) {
        if(val === oldVal) return; // Skip inital call
        // console.log('change');
        element[val ? 'fadeIn' : 'fadeOut'](1000);
      });
    }
  }
}

angular.module('app', []).controller('VerticalsController', VerticalsController).directive('uiFadeToggle', fadeToggleDirective);

angular.bootstrap(document.body, ['app']);    angular.bootstrap(document.body, ['app']);

HTML:

<h1>Pricing Poll</h1>
<div ng-controller="VerticalsController">
  <div id="container" ui-fade-toggle="visible">
    <h2>{{currentVertical.title}}</h2>

    <ul>
      <li ng-repeat="pricing in currentVertical.pricings">
        <a ng-click="selectPricing(pricing)">{{pricing.name}}</a>
      </li>
    </ul>
  </div>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular JS - 如何在模型更改时制作动画? 的相关文章

  • 带有绝对路径的角度js templateUrl

    在带有 url 的视频观看页面上 观看 video id 我有一个 ng 应用程序 该指令如下所示 app directive myApp function return restrict E templateUrl ng template
  • 如何重置表单中的 $dirty

    我在使用时遇到一个问题 dirty在我的申请表中 问题是一旦您更改表单字段 值 dirty将被设置为true但现在当您撤消更改时 它不会重置 dirty价值false 我们可以重置 dirty价值false手动但之后当您再次更改表单字段值时
  • 如何并排展开和折叠三个div?

    document ready function toggle click function if this data name show sidebar animate width 10 hide map animate width 89
  • 在 Angularjs 中格式化输入值

    我正在尝试编写一个指令 自动格式化数字
  • 按 Chartjs 条形图的键对对象进行分组

    我正在尝试使用 Chart js 创建条形图 我在尝试根据每个用户的状态创建分组条形图时陷入困境 这是数据 statusId 0 firstName Joe status appealed count 1 statusId 0 firstN
  • 有效使用 Angular Promise 和 Defers

    在 Angular 以及所有 SPA JS 框架 中 假设页面导航对于用户来说非常快速且 无缝 此速度的唯一瓶颈是使用 API 调用从我们的服务器检索数据 因此 寻找一种解决方案似乎是合理的 在该解决方案中 我们可以在等待 API 调用获得
  • 将安全的 Grafana 嵌入到 Web 应用程序中

    我想使用 AngularJS 将 Grafana 嵌入到我的 Web 应用程序中 目标是 当用户使用我的应用程序时 她应该能够单击按钮并加载 Grafana UI 就其本身而言 这是一项简单的任务 为此 我使用 apache 代理 Graf
  • AngularJS,使用没有后退按钮刷新的路由

    我在用着angularJS使用 AJAX 构建一个简单的单页应用程序 但是当用户使用本机后退按钮时我遇到了问题 angular module myApp ionic myApp controllers myApp services conf
  • ng-include 跨域帮助 angularjs

    我正在尝试为加载到 ng include 跨域的 url 实现白名单 这是一场噩梦 我有一个在本地运行良好的包含 div div 然后我像这样添加白名单 angular module myApp ngRoute ngResource con
  • 如何识别 YouTube 播放器的音量变化

    我正在使用 angualr youtube embed 指令将 YouTube 播放器嵌入到我的 Angular Web 应用程序中 因为我必须识别播放和暂停以及音量变化事件 为了监听播放和暂停事件 我使用下面给出的代码 scope on
  • 将行推入使用 ng-repeat 以角度呈现的表格中

    当客户端单击该行时 我想在表中插入额外的行 不应预取数据 因为我预计最多有 30 行 但每行都有关联的数据 在一次获取中获取这些数据是不合理的 到目前为止 我的方法是使用 ng repeat 迭代我的集合并渲染表格 当客户端按下该行时 客户
  • 使用 Phaser.js 和 Ionic 开发游戏应用程序(渲染缓慢/不稳定)

    只是为了让您知道 以防有人想要开发 我使用 Phaser js 开发了一个游戏应用程序 我将代码放入 Ionic 空白启动应用程序中 所以基本上视图是使用 Ionic 应用程序渲染的 然后 Phaser 通过 id 选取 div 并显示游戏
  • Angular JS 在调用新的 $http 之前取消 $http 调用

    在 Angular JS 1 1 5 中 您可以取消之前启动的 http 调用 这两个link1 https stackoverflow com questions 16962232 in angularjs how to stop ong
  • 在 History popstate 事件中获取 AngularJs $scope?

    我是 angularjs 开发新手 我有 var app angular module myapp app controller ProductCtrl scope sce function scope sce scope products
  • 从指令动态地将角度属性添加到元素

    我正在尝试构建一个指令来更改缓慢的 ajax 调用按钮上的加载状态 基本上 这个想法是为按钮元素设置一个属性 ng loading 并让指令添加其余的内容 这是html代码
  • 在角度控制器中监听文档事件

    如何捕获角度控制器中的事件 我有文档级事件 所以我需要在角度控制器中捕获事件 这可能吗 Update 我有独立的 js 文件来处理来自相机的一些操作 document addEventListener myCameraEvent handl
  • 如何销毁角度工厂实例

    一方面 我有几个工厂 每个工厂都控制一个 websocket 另一方面 其中一个工厂应该在客户端登录时启动 因此 if user isLogged injector get NotificationsWebsocket 这就是我动态初始化工
  • 用户脚本 - 有没有办法将 jquery 代码注入 angularjs dom?

    所以我正在尝试为一个网站创建用户脚本 我无法更改网站的任何源代码 因为它不是我的 网站到处都使用 AngularJS 控制器 我研究了几天如何做到这一点 但没有成功 所以我尝试注入代码 nav after div test div 当我通过
  • Protractor+AngularJS+Jasmine - 测试按住项目

    AngularJS 和 Protractor 非常新 但我认为到目前为止我正在朝着正确的方向前进 我的网站有一个项目列表 当您单击该项目并按住 X 秒时 它会打开一个模式窗口 我如何在 Protractor Jasmine 中模拟这种行为
  • Protractor e2e 测试表头和 , 标签

    我正在使用下表 我想测试每个标签 th td 标签 该标签中的文本以及该文本的计数 HTML 片段 table class table table striped tbody tr th b a Patient Id a b th th b

随机推荐