AngularJSAngularJS1.X就学笔记2-多少绑定

  上一篇从完整上认识了Angular,从今日开端一发一语道破的读书Angular的特色。本次学习的是数额绑定。应该具备的MVC框架都会用到数码绑定,比如我所理解的ThinkPHP、struts等,唯有完成了数量绑定才能将模型层和视图层分离,已毕MVC。Angular的数目绑定相比较特别,它协理双向数据绑定。

1、ng-bind

 

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding1</title>
</head>
<body>
    <h1 ng-controller='dataCtrl' ng-bind='data'>

    </h1>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "你好啊!";
        })
    </script>
</body>
</html>

  ng-bind已毕了一个不难单向绑定。

2、{{}}

  类似ng-bind,用的可比多。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding1</title>
</head>
<body>
    <h1 ng-controller='dataCtrl'>
        {{data}}
    </h1>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "你好啊!";
        })
    </script>
</body>
</html>

 

  那种绑定的欠缺是,起先加载时恐怕会产出就像是{{data}}那样的东西。

AngularJS 1

  化解措施是应用ng-bind或ng-cloak,ng-cloak应该只在有多少绑定的地点使用,否则处理中用户将见到空白。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding2</title>
</head>
<body ng-cloak>
    <h1 ng-controller='dataCtrl'>
        {{data}}
    </h1>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "你好啊!";
        })
    </script>
</body>
</html>

 

   我测试了一晃ng-cloak,不知晓为什么不行,有人知道的话请告知一下。

3、ng-bind-html

  那个命令可以用html的艺术处理数据,它不会将html代码解析成实体。下边相比一下ng-bind和ng-bind-html.

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding3</title>
</head>
<body>
    <div ng-controller='dataCtrl' ng-bind='data'>

    </div>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "<h1 style='color:red;'>你好啊</h1>";
        })
    </script>
</body>
</html>

AngularJS 2

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding3</title>
</head>
<body>
    <div ng-controller='dataCtrl' ng-bind-html='data'>

    </div>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "<h1 style='color:red;'>你好啊</h1>";
        })
    </script>
</body>
</html>

 

  换成ng-bind-html时出错了

AngularJS 3

  那是因为Angular默许是不信任html的,所以要那样做。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding3</title>
</head>
<body>
    <div ng-controller='dataCtrl' ng-bind-html='data'>

    </div>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope,$sce){
            $scope.data = $sce.trustAsHtml("<h1 style='color:red;'>你好啊</h1>");
        })
    </script>
</body>
</html

  那样就足以了。

AngularJS 4

4、ng-bind-template

  ng-bind只接受单个数据绑定表达式,而ng-bind-template则相对灵活些。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding3</title>
</head>
<body>
    <div ng-controller='dataCtrl' ng-bind-template='{{data1}}爱{{data2}} '>

    </div>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope,$sce){
            $scope.data1 = "我";
            $scope.data2 = "中国";
        })
    </script>
</body>
</html>
<!-- 我爱中国-->

 

5、ng-non-bindable

  有时大家采取了{{}}不过我们并不是要绑定数据,直接用会出错,所以要像那样

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding1</title>
</head>
<body>
    <h1 ng-controller='dataCtrl' ng-non-bindable>
        ng中绑定数据的方法是{{data}}
    </h1>

    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            //$scope.data = "你好啊!";
        })
    </script>
</body>
</html>

 

6、双向数据绑定ng-model

  双向数据绑定允许成分从用户处收集数据以改变程序状态。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>databinding5</title>
</head>
<body>
    <div ng-controller='dataCtrl'>
        <h1>{{data}}</h1>
        <input type="text" name="data" ng-model="data">
    </div>


    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module('myApp',[])
        .controller('dataCtrl',function($scope){
            $scope.data = "你好啊!";
        })
    </script>
</body>
</html>

 

AngularJS 5

  你会意识文本框的始末和h1中的内容同步转移。

7、小结一下

AngularJS,  与数量绑定的连带指令如下

  AngularJS 6

  上边起先攻读模板指令。2017-03-31
20:41:59

相关文章