search
HomeWeb Front-endJS TutorialHow to use Angular ng-animate and ng-cookies within the project

This time I will show you how to use Angular ng-animate and ng-cookies in the project. What are the precautions for using Angular ng-animate and ng-cookies in the project. The following is a practical case. Let’s take a look. .

ng-animate

This article talks about the animation application part in Angular.

First of all, Angular does not provide an animation mechanism natively. You need to add the Angular plug-in module ngAnimate to the project to complete Angular's animation mechanism. Angular does not provide specific animation styles, so its degree of freedom and usability It’s quite customizable.

So, you first need to introduce the Angular framework (angular.js) into the entry html file of the project, and then introduce angular.animate.js.

In the project's js entry file app.js, create a new project module and add the dependent module ng-Animate (if there are other required modules, you can also introduce them, the order does not matter)

var demoApp = angular.module('demoApp', ['ngAnimate','ui.router']);

Insert a sentence in the middle here. It is recommended to use the following mode for dependency injection in Angular. There will be no problem after packaging and compression with ads, bds or other front-end automation tools, because dependencies are only injected in the form of passing parameters to the function. Angular will There are strict requirements for injected variable names (for example, when the $scope variable name is injected in the controller, the variable name can only be written as $scope):

//控制器.js、指令.js、过滤器.js的依赖注入建议都用这种方式写
//这是ui-route的配置,在app.js
demoApp.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider){
 // your code.
}]);

Okay, back to the topic. After introducing ngAnimate, Angular's animation mechanism can take effect.

The Angular document writes the following instructions and supported animations

So, how to use it? This article uses the ng-repeat instruction as an introduction. Some other instructions are used in almost the same way and can be deduced by analogy.

ng-repeat is mainly for displaying a list. These elements are created and added to the DOM structure. Then, its animation process is:

Create elements-> ; .ng-enter -> .ng-enter-active -> Completed, in default state

Default state-> .ng-leave -> .ng-leave-active -> Destroyed Element

So you can display animation by setting the styles of .ng-enter(.ng-leave) and .ng-enter-active(.ng-leave-active), plus css3 animation, such as :

<!-- HTML片段 -->
<p></p>
<input>
  •  {{user}}
/* css片断 */ /*ng-repeat的元素*/ .item{  -webkit-transition: all linear 1s;  -o-transition: all linear 1s;  transition: all linear 1s; } /*动画开始前*/ .item.ng-enter{  opacity:0; } /*动画过程*/ .item-ng-enter-active{  opacity:1; }

This effect is applied to all elements at the same time. In actual application, there may be a sequential gradient effect. In this case, ng-enter-stagger can be set to achieve it.

/*不同时出现*/
.item.ng-enter-stagger {
 transition-delay:0.5s;
 transition-duration:0s;
}

Similarly, these animation classes provided by angular animate can also be applied to page switching. Custom animation (based on class)

Custom animation when adding and removing class

.class-add -> .class-add-active -> .class

If writing css still cannot meet the needs, of course, you can also control animation through JS. You can understand the following code as a template

/* CLASS 是需要应用的class名,handles是支持的操作,如下图所示*/
/* 这里如果是应用在ui-view 的class上,模版会叠加(坑)*/
demoApp.animation('.classname',function(){
return {
 'handles':function(element,className,donw){
  //... your code here
  //回调
  return function(cancelled){
  // alert(1);
  }
 }
 }
})

supported Operation:

##ng-cookies

$cookies[name] = value;
This is the angular cookie setting method

$cookieStore

Provides a key-value pair (string-object) storage supported by session cookies. Objects being stored and retrieved will automatically be serialized/deserialized through angular's toJson/fromJson.

$cookies

Provides read/write access operations for browser cookies.

Both of these must be used by introducing the ngCookies module. This module has been available since version 1.4.

It is known from the source code that $cookieStore returns three methods get put remove and they are used respectively. toJson/fromJson for serialization/deserialization

I simply wrote a few examples to test

nbsp;html>

 
  <meta>
  <title></title>
 
 <script></script>
 <script></script>

  {{title}}
 
 <script>
  var AutumnsWindsApp = angular.module(&#39;AutumnsWindsApp&#39;, [&#39;ngCookies&#39;]);
  AutumnsWindsApp.controller(&#39;aswController&#39;, function($cookies, $cookieStore, $scope) {
   $cookies.name = &#39;autumnswind&#39;;
   $scope.title = "Hello, i&#39;m autumnswind :)";
   $cookieStore.put("skill", "##");
   //删除cookies
   $cookieStore.remove("name");
   //设置过期日期
   var time = new Date().getTime() + 5000;
   $cookieStore.put("cookie", "Hello wsscat", {
    expires: new Date(new Date().getTime() + 5000)
   });
   $cookieStore.put("objCookie", {
    value: "wsscat cat cat",
    age: "3",
 }, {
  expires: new Date(new Date().getTime() + 5000)
   });
   console.log($cookies);
   console.log($cookies[&#39;objCookie&#39;]);
  })
 </script>
In fact, we can usually set the cookies we need in this way

$cookies.name = 'autumnswind';
But when we want to set a valid time, we use this method to set it in

var time = new Date().getTime() + 5000;
   $cookieStore.put("cookie", "Hello wsscat", {
    expires: new Date(new Date().getTime() + 5000)
   });
We can also perform operations such as deletion

$cookieStore.remove("name");

Supplement:

ng-repeat-track by Usage:

<p>
 </p><p>{{link.name}}</p>

Error: [ngRepeat:dupes]这个出错提示具体到题主的情况,意思是指数组中有2个以上的相同数字。ngRepeat不允许collection中存在两个相同Id的对象

For example: item in items is equivalent to item in items track by $id(item). This implies that the DOM elements will be associated by item identity in the array.
对于数字对象来说,它的id就是它自身的值,因此,数组中是不允许存在两个相同的数字的。为了规避这个错误,需要定义自己的track by表达式。例如:

item in items track by item.id或者item in items track by fnCustomId(item)。
嫌麻烦的话,直接拿循环的索引变量$index来用item in items track by $index

自定义服务的区别:

factory()----函数可以返回简单类型、函数乃至对象等任意类型的数据 一般最为常用
service()-----函数数组、对象等数据
factory和service不同之处在于,service可以接收一个构造函数,当注入该服务时通过该函数并使用new来实例化服务对象

constant()----value()方法和constant()方法之间最主要的区别是,常量可以注入到配置函数中,而值不行,value可与你修改,constant不能修改

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS实现输入框内灰色文字提示

react-redux插件项目实战使用解析

The above is the detailed content of How to use Angular ng-animate and ng-cookies within the project. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
聊聊Angular中的元数据(Metadata)和装饰器(Decorator)聊聊Angular中的元数据(Metadata)和装饰器(Decorator)Feb 28, 2022 am 11:10 AM

本篇文章继续Angular的学习,带大家了解一下Angular中的元数据和装饰器,简单了解一下他们的用法,希望对大家有所帮助!

angular学习之详解状态管理器NgRxangular学习之详解状态管理器NgRxMay 25, 2022 am 11:01 AM

本篇文章带大家深入了解一下angular的状态管理器NgRx,介绍一下NgRx的使用方法,希望对大家有所帮助!

浅析angular中怎么使用monaco-editor浅析angular中怎么使用monaco-editorOct 17, 2022 pm 08:04 PM

angular中怎么使用monaco-editor?下面本篇文章记录下最近的一次业务中用到的 monaco-editor 在 angular 中的使用,希望对大家有所帮助!

项目过大怎么办?如何合理拆分Angular项目?项目过大怎么办?如何合理拆分Angular项目?Jul 26, 2022 pm 07:18 PM

Angular项目过大,怎么合理拆分它?下面本篇文章给大家介绍一下合理拆分Angular项目的方法,希望对大家有所帮助!

Angular + NG-ZORRO快速开发一个后台系统Angular + NG-ZORRO快速开发一个后台系统Apr 21, 2022 am 10:45 AM

本篇文章给大家分享一个Angular实战,了解一下angualr 结合 ng-zorro 如何快速开发一个后台系统,希望对大家有所帮助!

聊聊自定义angular-datetime-picker格式的方法聊聊自定义angular-datetime-picker格式的方法Sep 08, 2022 pm 08:29 PM

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

聊聊Angular Route中怎么提前获取数据聊聊Angular Route中怎么提前获取数据Jul 13, 2022 pm 08:00 PM

Angular Route中怎么提前获取数据?下面本篇文章给大家介绍一下从 Angular Route 中提前获取数据的方法,希望对大家有所帮助!

浅析Angular中的独立组件,看看怎么使用浅析Angular中的独立组件,看看怎么使用Jun 23, 2022 pm 03:49 PM

本篇文章带大家了解一下Angular中的独立组件,看看怎么在Angular中创建一个独立组件,怎么在独立组件中导入已有的模块,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)