Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

เรียก encodeURIComponent จาก AngularJS Template ใน HTML

สร้าง Custom Filter

HTML Template

<div ng-app="app">
  <a href="/foo/{{ 'bar&baz' | encodeURIComponent }}">Demo</a>
</div>

AngularJS Module

var app = angular.module('app', []);
app.filter('encodeURIComponent', function() {
    return window.encodeURIComponent;
});

Filter นี้จะเรียก window.encodeURIComponent ทุกครั้งที่ Expression ถูก evaluate