AngularJS-study亦混点对比-01

ng-bind VS ng-bind-template

ng-bind:

<h2 ng-bind="name"></h2>

ng-bind-template

<h2 ng-bind-template="{{name}} is {{age}}"></h2>

ng-bind-template在指令中可以使用模板,显示的话必须是表达式语法,否则在这里面都是字符串

ng-include

<div ng-include="'footer.html'"> 

ng-include可以引入其他页面,注意一定要加'',否则会当成变量

$scope VS controller-as

$scope

controller('MainCtrl',['$scope',function($scope){
    //声明一个变量
    $scope.name = "Tom";
}

controller-as

视图中

<body ng-controller="MainCtrl as ctrl">
<h2>使用ctrl <span>{{ ctrl.name }}</span></h2>

控制器中

 controller('MainCtrl',[function(){
    //声明一个变量
   var self = this;
   self.name = "Tom";
}

ng-href VS ng-src

这个不多讲了,直接上代码

<a ng-href="{{ ctrl.obj.href }}" ng-bind="ctrl.obj.href"></a>
![]({{ ctrl.obj.src }})
<script type="text/javascript">
    angular.module('myApp',[])
        .controller("MainCtrl",[function(){
            var self = this;
            self.obj = {
                href:"http://www.baidu.com",
                src:"C://Users/Administrator/Desktop/无标题.png"
            }
        }])
</script>

ng-show VS ng-hide

<!-- ng-show--当条件为true的时候显示 -->
<h2  ng-show="ctrl.flag">showH2</h2>
<!-- ng-hide--当条件为true的时候显示 -->
<h2  ng-hide="ctrl.flag">hideH2 </h2>

ng-class的两种用法

1、通过对象数组的方法

视图

<div ng-class="{true:'change1',false:'change2'}[className]"></div>

控制器

<script>
  var app=angular.module("myModule",[])
   app.controller('firstController',function($scope){
     $scope.className=true;
   })
</script>

实现很简单,就是当className为true的时候class为change1,相反则为change2。
但是有一点不好的只能够让一个元素拥有两种状态

2、通过key/value

视图

<div ng-class{'class1':ctrl.select1,'class2':ctrl.select2,'class3':ctrl.select3}">
    hello world
</div>

控制器

angular.module('myApp',[])
        .controller('MyCtrl',[function(){
            var self = this;
            self.select1 = true;
        }]);

当select1为true的时候,class则为class1,个人认为这个是比较推荐的,可以弥补第二种方式的缺憾~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • 通过AngularJS仿豆瓣一刻的案例:https://github.com/zhongxiaolian/doub...
    中小恋阅读 1,782评论 1 21
  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 1,647评论 0 3
  • AngularJS AngularJS概述 介绍 简称:ng Angular是一个MVC框架 其他前端框架: Vu...
    我爱开发阅读 2,347评论 0 8
  • 基础ng属性指令 布尔属性 布尔属性代表一个true或false值。当这个属性出现时,这个属性的值就是true(无...
    oWSQo阅读 1,244评论 0 0