源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

angularjs中ng-attr的用法详解

  • 时间:2022-07-21 01:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:angularjs中ng-attr的用法详解
[b]前言[/b] html中的属性很多,同样可以使用angularjs来定义: [b]ng-attr-(suffix)=只能使用变量定义[/b]
<div title="angularjs中的title">title</div>
<div ng-attr-title="angularjs中的title">title</div><!--这样写显示不出来标题-->
<div ng-attr-title="'angularjs中的title'">title</div><!--这样写显示不出来标题-->
<div ng-attr-title="{{titleStr}}">title</div><!--只能使用变量定义-->
angular.module('myApp',[])
   .controller('myCtrl',['$scope',function($scope){
       $scope.titleStr = "angularjs中的title";
   }]);
ng-bind中使用字符可以将文字显示出来
<span ng-bind=" 'angularjs中的title'  ";
[b]用法实例代码:[/b]
<!DOCTYPE html>
<html lang="zh-CN" ng-app="app">
<head>
  <meta charset="utf-8">
  <title>ng-attr-(suffix)的用法</title>
  <link rel="stylesheet" href="../bootstrap.min.css">
</head>
<body>
  <div>
    <p>1.正确做法:</p>
    <input type="text" ng-model="suffix" placeholder="请输入input的type值">
    (如:checkbox,radio,button,submit...)
    <br><br>
    我将随着输入的值变化:<input ng-attr-type="{{ suffix }}"> 
  </div>
  <div style="margin-top: 20px;">
    <p>2.错误做法:cx="{{ cx }}"</p>
    <p>这里的错误做法,并非真正的错误做法,只是有时浏览器会对属性会进行很苛刻的限制,所以不建议这样做。比如svg</p>
    <div style="border: 1px solid red;width: 200px;height: 200px;">
      <svg>   
        <circle cx="{{ cx }}"></circle> 
      </svg> 
      这里浏览器会报错 
    </div>
    
    改为:ng-attr-cx="{{ cx }}"
    <div style="border: 1px solid green;width: 200px;height: 200px;">
      <svg>   
        <circle ng-attr-cx="{{ cx }}"><circle>
      </svg>
      这里不会报错
    </div>
    
  </div>
  

  
  <script src="../angular.min.js"></script>
  <script>
    angular.module('app', [])
  </script>
</body>
</html>
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部