如何在AngularJS模板中调用encodeURIComponent?

WBOY
发布: 2023-09-05 16:57:07
转载
1372人浏览过

如何在angularjs模板中调用encodeuricomponent?

在本文中,我们将学习如何从 HTML 中的 angularjs 模板调用编码 URI 组件。

每当某个字符出现在 URI 中时,encodeURIComponent() 函数就会将其替换为一个、两个、三个或四个表示该字符的 UTF-8 编码的转义序列(只能是由两个“代理”字符组成的字符的四个转义序列)。

语法

以下是encodeURIComponent的语法

encodeURIComponent(uriComponent)
登录后复制

Uri组件

任何对象,包括字符串、数字、布尔值、null 或未定义。 uriComponent在编码之前转换为字符串。

让我们看一下以下示例以更好地理解。

示例 1

在下面的示例中,我们使用encodeURI组件

AiPPT模板广场
AiPPT模板广场

AiPPT模板广场-PPT模板-word文档模板-excel表格模板

AiPPT模板广场 147
查看详情 AiPPT模板广场
<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <script>
      let uri = "https://www.tutorialspoint.com/index.htm";
      let encoded = encodeURIComponent(uri);
      document.getElementById("tutorial").innerHTML = encoded;
   </script>
</body>
</html>
登录后复制

运行上述脚本时,会弹出输出窗口,显示我们在上述脚本中使用的 URL 的编码 URL。

示例 2

在下面的示例中,我们使用函数 encodeURIcomponent(string) 对 url 参数进行编码。

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
   <script>
      var myApp = angular.module("mytutorials", []);
      myApp.controller("mytutorials1", function($scope) {
         $scope.url1 = 'https://www.tutorialspoint.com/index.htm';
         $scope.url2 = '';
         $scope.encodeUrlStr = function() {
            $scope.url2 = encodeURIComponent($scope.url1);
         }
      });
   </script>
</head>
<body>
   <div ng-app="mytutorials">
      <div ng-controller="mytutorials1">
         <button ng-click ="encodeUrlStr()" >Encode URL</button>
         <br>
         URL1 = {{url1}}<br>
         URL2 = {{url2}}
      </div>
   </div>
</body>
</html>
登录后复制

当脚本执行时,它将生成由 url1 和 url2 组成的输出,该输出为空,并在网页上显示一个encodeURL 按钮。

如果用户点击encodeURL按钮,url1中给出的url将被编码并显示在url2中。

以上就是如何在AngularJS模板中调用encodeURIComponent?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号