Angular中ng-bind和ng-model的区别实例详解
程序员文章站
2022-04-29 08:57:39
ng-bind和ng-model的区别
angularjs的数据绑定有ng-bind和ng-model,一般用于如下:
ng-bind和ng-model的区别
angularjs的数据绑定有ng-bind和ng-model,一般用于如下:
<input ng-model="object.xxx">
<span ng-bind="object.xxx"></span>
ng-bind是单向绑定,由作用于$scope到view层,且在html控件(html控件有:input、select、button和textarea)中不可显示。
ng-model是双向绑定,$scope<--->view层。
{{}}用于显示数据。
完整代码
<!doctype html> <html> <meta charset="utf-8"> <head> <style> div textarea{ background-color:yellow; } </style> <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="mynoteapp"> <div class="aa" ng-controller="mynotectrl"> <h2>我的笔记</h2> <p> <textarea ng-model="message" rows="10" cols="20" maxlength="100"> </textarea> </p> <p> <button ng-click="save()">保存</button> <button ng-click="clear()">清除</button> </p> <p>number of characters left: <span ng-bind="left()"></span></p> <p>number of characters left(method2): <span>{{leftvar}}</span></p> </div> <script> var app = angular.module("mynoteapp",[]); app.controller("mynotectrl",function($scope){ $scope.message = ""; $scope.leftvar = 0; $scope.left = function() {$scope.leftvar = 100 - $scope.message.length;return $scope.leftvar;}; $scope.clear = function() {$scope.message = "";}; $scope.save = function() {alert("note saved");}; }); </script> </body> </html>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
ubuntu系统中/etc/rc.local和/etc/init.d/rc.local的区别详解
-
详解nodejs中exports和module.exports的区别
-
Angular中$cacheFactory的作用和用法实例详解
-
jsp中include指令静态导入和动态导入的区别详解
-
裸链和锚链的区别在哪?外链建设中裸链和锚链的作用分析详解
-
MySQL中datetime和timestamp的区别及使用详解
-
详解Node.js中path模块的resolve()和join()方法的区别
-
jQuery中的on与bind绑定事件区别实例详解
-
Angular.js中ng-if、ng-show和ng-hide的区别介绍
-
详解Python中__str__和__repr__方法的区别