码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • angularjs directive(指令用法)将int转化成string


    1)将int转化成string

    a)angularjs select在ng-model值为int的时候无法实现双向绑定

    b)通过directive(指令用法)将int转化成string就能实现双向绑定

    1. DOCTYPE html>
    2. <html ng-app="my_app">
    3. <head>
    4. <script src="angular.min.js">script>
    5. <script type="text/javascript">
    6. var app = angular.module("my_app", []);
    7. app.controller('my_controller', function($scope) {
    8. $scope.type = 2;
    9. $scope.typeList = [{
    10. code: 1,
    11. name: '分期付款'
    12. }, {
    13. code: 2,
    14. name: '一次性付款'
    15. }];
    16. }).directive("toString", function() {
    17. return {
    18. restrict: "A",
    19. require: "ngModel",
    20. link: function(scope, elem, attr, ngModelCtr) {
    21. ngModelCtr.$formatters.push(function(viewValue) {
    22. if (typeof viewValue != "undefined") {
    23. return viewValue.toString();
    24. }
    25. })
    26. }
    27. }
    28. });
    29. script>
    30. head>
    31. <body ng-controller="my_controller">
    32. <select ng-model="type" to-String>
    33. <option ng-repeat="item in typeList" value="{{item.code}}">
    34. {{item.name}}
    35. option>
    36. select>
    37. body>
    38. html>

    2)将string转化成int

    a)angularjs input=number在ng-model值为string的时候无法实现双向绑定

    b)通过directive(指令用法)将string转化成int就能实现双向绑定

    1. DOCTYPE html>
    2. <html ng-app="my_app">
    3. <head>
    4. <script src="angular.min.js">script>
    5. <script type="text/javascript">
    6. var app = angular.module("my_app", []);
    7. app.controller('my_controller', function($scope) {
    8. // 绑定值是数字
    9. $scope.value1 = 1.1;
    10. // 绑定值是字符串
    11. $scope.value2 = '2.2';
    12. }).directive('stringToNumber', function() {
    13. return {
    14. require: 'ngModel',
    15. link: function(scope, element, attrs, ngModel) {
    16. ngModel.$parsers.push(function(value) {
    17. return '' + value;
    18. });
    19. ngModel.$formatters.push(function(value) {
    20. if (value)
    21. return Number(value);
    22. return value;
    23. });
    24. }
    25. };
    26. });
    27. script>
    28. head>
    29. <body ng-controller="my_controller">
    30. <input type="number" ng-model="value1" />
    31. <input type="number" ng-model="value2" string-to-number/>
    32. body>
    33. html>

  • 相关阅读:
    [Git] 系列三随意修改提交记录以及一些技巧
    ch15、面向对象编程-行为的定义和实现
    计算机网络4——网络层2
    Linux Shell编程第5章——文件的排序、合并和分割
    Win32 键盘鼠标模拟输入
    数学分析:傅里叶三角级数
    在Linux下安装MySQL
    Windows网络管理及诊断命令整理
    zookeeper整合ElasticJob实现任务调度
    给出一组正整数arr,你从第0个数向最后一个数,每个数的值表示你从这个位置可以向右跳跃的最大长度,计算如何以最少的跳跃次数跳到最后一个数。
  • 原文地址:https://blog.csdn.net/czh4869623/article/details/133030603
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号