Difference between revisions of "Lập trình NodeJS và AngularJS"

From CodeForLife
Jump to: navigation, search
Line 28: Line 28:
 
**'''VD2''': Thêm link với thuộc tính target=_blank để link được mở trong tab mới.<br/> <span style="color:#0000FF;">''var someText = "this is a text with a link www.github.com ..";<br/> var result = anchorme.js(someText, {"target":"_blank"});''</span>  
 
**'''VD2''': Thêm link với thuộc tính target=_blank để link được mở trong tab mới.<br/> <span style="color:#0000FF;">''var someText = "this is a text with a link www.github.com ..";<br/> var result = anchorme.js(someText, {"target":"_blank"});''</span>  
 
**'''VD3''': Thêm link với nhiều lựa chọn hơn:<br/> <span style="color:#0000FF;">''var someText = "this is a text with a link www.github.com ..";<br/> var result = anchorme.js(someText, {"class":"someclassname","id":"someID","target":"_blank", "attribute-name":"attribute-property"});''</span>   
 
**'''VD3''': Thêm link với nhiều lựa chọn hơn:<br/> <span style="color:#0000FF;">''var someText = "this is a text with a link www.github.com ..";<br/> var result = anchorme.js(someText, {"class":"someclassname","id":"someID","target":"_blank", "attribute-name":"attribute-property"});''</span>   
 +
  
 
=== '''Hiển thị HTML trong AngularJS''' ===
 
=== '''Hiển thị HTML trong AngularJS''' ===
Line 41: Line 42:
 
*'''B2''': Trong tệp html include thư viện '''angular-sanitize'''. Ví dụ như sau:<br/> <span style="color:#0000FF;">''<script src="node_modules/angular-sanitize/angular-sanitize.min.js"></script>''</span>  
 
*'''B2''': Trong tệp html include thư viện '''angular-sanitize'''. Ví dụ như sau:<br/> <span style="color:#0000FF;">''<script src="node_modules/angular-sanitize/angular-sanitize.min.js"></script>''</span>  
 
*'''B3''': Include mô đun&nbsp;'''<code>ngSanitize</code>&nbsp;'''vào ứng dụng '''app'''. Ví dụ như dưới:<br/> <span style="color:#0000FF;">''var app = angular.module('myApp', ['ngSanitize']);''</span>  
 
*'''B3''': Include mô đun&nbsp;'''<code>ngSanitize</code>&nbsp;'''vào ứng dụng '''app'''. Ví dụ như dưới:<br/> <span style="color:#0000FF;">''var app = angular.module('myApp', ['ngSanitize']);''</span>  
−
*'''B4''': Khi sử dụng bạn chỉ cần bind giá trị vào thuộc tính&nbsp;'''ng-bind-html''' của một thẻ html nào đó là được. Ví dụ: <div><span style="color:#0000FF;">''<div ng-bind-html="preview_data.preview.embed.html"></div><br/> <span ng-bind-html="msg.text"></span>''</span></div> 
+
*'''B4''': Để tránh lỗi [$sce:unsafe], bạn thêm đoạn code sau vào ứng dụng apps:<br/> <span style="color:#0000FF;">''app.filter('to_trusted', ['$sce', function($sce){<br/> &nbsp; &nbsp; return function(text) {<br/> &nbsp; &nbsp; &nbsp; &nbsp; return $sce.trustAsHtml(text);<br/> &nbsp; &nbsp; };<br/> }]);''</span>
 +
*'''B5''': Khi sử dụng bạn chỉ cần bind giá trị vào thuộc tính&nbsp;'''ng-bind-html''' của một thẻ html nào đó là được. Ví dụ:<br/> <div class="airdrop-description" ng-bind-html="data.info.description"></div>  
 +
 
 +
<span style="color:#FF0000;">'''Chú ý''': Trong một số bản Angular mới, đôi khi không cần bước B1, B2 và B3.</span>
  
 
=== '''Nén dữ liệu trả về''' ===
 
=== '''Nén dữ liệu trả về''' ===

Revision as of 15:35, 24 October 2018

Lập trình NodeJS

 

Lập trình AngularJS

 

Một số tác vụ đặc biệt

Website hỗ trợ nhiều ngôn ngữ

 

Tự động thêm link vào URL trong một đoạn văn bản

Trong nhiều trường hợp, khi hiển thị text lên trình duyệt, bạn muốn tự động thêm thẻ link cho các URL có trong văn bản. Trong trường hợp này bạn có thể sử dụng thư viện anchorme: https://github.com/Jpja/CounterTools/tree/master/lib/anchorme

Các bước thực hiện:

  • B1: Tải tệp anchorme.min.js từ địa chỉ trên
  • B2: Include vào trong tệp html:
    <script src="anchorme.min.js"></script>
  • B3: Trong code javascript, gọi hàm anchorme.js là xong.
    • VD1: Tự động thêm link bình thường
      var someText = "this is a text with a link www.github.com ..";
      var result = anchorme.js(someText);
    • VD2: Thêm link với thuộc tính target=_blank để link được mở trong tab mới.
      var someText = "this is a text with a link www.github.com ..";
      var result = anchorme.js(someText, {"target":"_blank"});
    • VD3: Thêm link với nhiều lựa chọn hơn:
      var someText = "this is a text with a link www.github.com ..";
      var result = anchorme.js(someText, {"class":"someclassname","id":"someID","target":"_blank", "attribute-name":"attribute-property"});


Hiển thị HTML trong AngularJS

Bạn tham khảo link sau:

Các bước thực hiện:

  • B1: Cài đặt angular-sanitize bằng lệnh sau:
    npm install angular-sanitize 
  • B2: Trong tệp html include thư viện angular-sanitize. Ví dụ như sau:
    <script src="node_modules/angular-sanitize/angular-sanitize.min.js"></script>
  • B3: Include mô đun ngSanitize vào ứng dụng app. Ví dụ như dưới:
    var app = angular.module('myApp', ['ngSanitize']);
  • B4: Để tránh lỗi [$sce:unsafe], bạn thêm đoạn code sau vào ứng dụng apps:
    app.filter('to_trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }]);
  • B5: Khi sử dụng bạn chỉ cần bind giá trị vào thuộc tính ng-bind-html của một thẻ html nào đó là được. Ví dụ:

Chú ý: Trong một số bản Angular mới, đôi khi không cần bước B1, B2 và B3.

Nén dữ liệu trả về

Việc nén dữ liệu trả về client sẽ giúp giảm thời gian dữ liệu truyền từ server về client, do đó làm tăng tốc độ ứng dụng web. Để giúp server hỗ trợ việc này, bạn thực hiện như sau:

  • Với Express 3, bạn thêm lệnh sau vào phần xử lý server:
    app.configure(function(){
        //....
        app.use(express.compress());
    });
  • Với Express 4+: Bạn cần cài đặt thêm gói riêng bằng lệnh sau:
    npm install compression
    Sau đó thêm code sau vào phần xử lý server:
    var compression = require('compression');
    app.use(compression());