Analysis of angular data display optimization processing

Source: Internet
Author: User

In the previous few articles, we use {{}} to render the data, and we'll talk about it today.

<div ng-controller= "Aaa" >
<div>{{text}}</div>
</div>
<script type= "Text/javascript" >
alert (123);
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
}]);
</script>

The problem is obvious, when we don't click OK, the following code is not executed, so also did not return to the status of {{text}}, assuming that the network bandwidth is very slow, the JS script has not loaded in, the page is all {{}} such symbols, obviously the user experience is obviously bad.

We use the Ng-bind directive to resolve this problem.

<div ng-controller= "Aaa" >
<div ng-bind= "Text" ></div>
</div>
<script type= "Text/javascript" >
Alert (123)
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
}]);
</script>

Isn't it great? The problem here, there is not only a text data, there are many, how to write it?

<div ng-controller= "Aaa" >
<div ng-bind-template= ' {text}},{{text}} ' ></div>
</div>
<script type= "Text/javascript" >
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
}]);
</script>

Ng-bind-template= ' {{text}},{{text}} ' multiple forms

Ng-bind= a single expression of "text"

Read the above introduction, if you still feel that the expression on the label is uncomfortable to be solved by ng-cloak ...

<div ng-controller= "Aaa" >
<div ng-cloak>{{text}}</div> Ng-cloak is none before rendering, block after rendering ends
</div>
<script type= "Text/javascript" >
alert (123);
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
}]);
</script>

The Ng-cloak is none before rendering, and the block after rendering ends.

Also add that when angular is introduced, a CSS style is embedded in the header.

Let's say the back end gives us a piece of text that contains the {{}} symbols that can be ng-non-bindable to mask angular parsing.

<div ng-controller= "Aaa" >
<div ng-non-bindable>{{text}}</div>
</div>
<script type= "Text/javascript" >
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
}]);
</script>

If it is a section of HTML code, want to parse the words need to introduce plug-ins ...

Complete code:

<! DOCTYPE html>
<metahttp-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Data display optimization processing </title>
<scripttype= "Text/javascript" src= "Https://code.angularjs.org/1.3.8/angular.min.js" ></script>
<scripttype= "Text/javascript" src= "Http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-sanitize.min.js" ></script>
<body>
<divng-controller= "Aaa" >
<divng-bind-html= "HTML" ></div>
</div>
<scripttype= "Text/javascript" >
var m1 = angular.module (' myApp ', [' ngsanitize ']); The introduction of angular plug-ins requires modules that rely on Plug-ins in the module
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. html = '
}]);
</script>
</body>

Here's how to handle styles and attributes in angular.

Style:

<div ng-class= "{red:true,yellow:true}" >{{text}}</div>
<div ng-controller= "Aaa" >
<div ng-style= "{{style}}" >{{text}}</div>
</div>
<script type= "Text/javascript" >
Varm1 = Angular.module (' myApp ', []);
M1.controller (' Aaa ', [' $scope ', function ($scope) {
$scope. Text = ' XIECG ';
Object-type data can also be rendered as inline styles
$scope. style = {
Color: ' Red ',
Background: ' Blue '
};
}]);
</script>

Property:

<div ng-controller= "Aaa" >
 <a ng-href= "{{URL}}" >baidu</a>
 <a ng-attr-href= ' {{URL}} ' ng-attr-title= ' {{text}} ' >baidu</a>
</div>
<script type= "Text/javascript" > /div>
 varm1 = angular.module (' myApp ', []);
 m1.controller (' Aaa ', [' $scope ', function ($scope) {
&NB SP; $scope. Text = ' XIECG ';
  $scope. url = ' https://www.baidu.com/';
 }];
</script>

A

Custom attribute can be written in such a form as ng-attr-**.

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.