Angular.js Study notes (ii)

Source: Internet
Author: User

Resolving the composition of URLs with a linked object
var url = ' Https://www.baidu.com:8080/aaa/1.html?id=10#name ';
var aLink = document.createelement (' a ');
Alink.href = URL;
Console.log (ALink);//<a href= "Https://www.baidu.com:8080/aaa/1.html?id=10#name" ></a>
Console.log (alink.search);//?id=10
Console.log (Alink.hash);//#name
...
Protocol:https
host:www.baidu.com:8080
Hostname:www.baidu.com
port:8080
Pathname:/aaa/1.html
Search:? id=10
Hash: #name
JSON is how a string describes an object

Angular.js Common directives:
The AngularJS property starts with ng-, but you can use data-ng-to make the page work for HTML5.

The NG-APP:NG-APP directive is used to mark a ANGULARJS application that is marked on the root object of a angularjs scope, and executes automatically by executing other instructions within the scope of the root object, and can create multiple NG-APP nodes on the same page, but is not recommended. When creating multiple Ng-app, the default is to perform only the first one, followed by manual booting: angular.bootstrap (Document.queryselector (' [ng-app= ' myApp2 '] '), [' myApp2 ']); But generally not guided in this way; the right way: angular.module (' myApp ', [' MyApp1 ', ' myApp2 ']);
Ng-bind: usage Example:
<!--<strong>{{username}}</strong>--
<strong ng-bind= "username" ></strong>
<!--ng-bind directives are escaped when bound values contain HTML for security (cross-site scripting attacks)-
If you do need to correctly input HTML, use ng-bind-html, and need to introduce the Angular-sanitize.js package, and create a module that relies on this package: Angular.module (' myApp ', [' ngsanitize ' ]); Finally make sure that you have the effect of using the module you define.
The ng-repeat:ng-repeat directive is used to compile an array to repeatedly create the current element, such as <li ng-repeat= "item in App" >
<strong>{{item.name}}</strong>
<span>{{item.age}}</span>
</li>
Error when there are duplicate values in the app, this should be the case: name in the app track by $index;
(Supplement: Name.startswith (LastName): The value of name starting with LastName is true----This is the H5 new element)
The Ng-class:ng-class directive can set a key-value pair that determines whether a particular class name is added, the key is a class name, and a bool type indicates whether to add the class name: Ng-class= "{red: $even, Green: $odd}"; Ng-class can also be used as normal class
Ng-show/ng-hide: Determines whether the current element is displayed based on the value of the attribute, for example Ng-show=false does not display the element;
Ng-if refers to the existence of DOM elements;
The ng-href/ng-src instruction is used to resolve a load bug that is caused when data binding of a link type
The ng-switch instruction shows or hides the corresponding part based on an expression. The corresponding child elements use the Ng-switch-when directive, and if the match is selected, the selection is displayed and the others are removed. You can set the default options by using the Ng-switch-default directive, and if none are matched, the default option is displayed.
The ng-checked directive is used to set the Checked property of a check box (checkbox) or radio button (radio). If the Ng-checked property returns True, the check box (checkbox) or radio button (radio) will be selected.
The ng-selected directive is used to set the selected property of the <option> element in the <select> list. The expression for the Ng-selected property returns True if the option is selected.
Common event directives and native JS basically the same, the difference is the front plus ng-; for example Ng-click;
Custom directives (important but not commonly used, which individuals find difficult):
Purpose: Encapsulate some common and common things, angular still have some dom operations possible, all DOM operations should be centralized in the custom directives.
The following demo default references bootstrap and angular
Demo1:
Script
var demoApp = angular.module (' demoApp ', []);
The first parameter is the name of the instruction, the second argument should use an array, the last element of the array is a function
Define the name of the directive, you should use the Hump naming method
Demoapp.directive (' Itcastbutton ', [function ()} {
The function should return a directive object
return {
Template: ' <input type= ' button ' value= ' itcast ' class= ' btn btn-lg btn-primary btn-block '/> '
};
}]);
Html:
<itcast-button></itcast-button>//replaces the content in the template
Demo2:
Script
Demoapp.directive (' btn ', [function ()} {
return{
scope:{
Primary: ' @ ',
LG: ' @ ',
Block: ' @ ',
},
Template: ' <button class= ' btn {{primary==\ ' true\ '? \ ' btn-primary\ ': \ ' \ '}} ' >button</button> '
}
}]);
Html:
&LT;BTN primary= "false" ></btn>
DEMO3:
Script
Demoapp.directive (' btn ', [function ()} {
return {
Transclude literally means embedding, which means that you don't need to embed the elements inside your instruction (note that the template is not a directive) into your template, and the default is False.
The transclude of the instruction object must be set to true to use the Ng-transclude directive in the template
Transclude:true,
Replace:true,//Replace directive elements bound in HTML
Template: ' <button class= ' btn btn-primary btn-lg ' ng-transclude></button> '
};
}]);
Html:
<btn>itcast</btn>
Demo4: (see some attribute meanings)
Script
Demoapp.directive (' breadcrumb ', [function ()} {
return {
Specifies the type of the current instruction what, all supported by default
Restrict: ' A ',
E = Element, A = Attribute, C = Class, M = Comment
Template: ',//template string
Templateurl: ' tmpls/breadcrumb.html ',//Introduction template
Replace:true,
Transclude:true,
};
}]);

Angular.js Study notes (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.