AngularJS入门(用ng-model指令实现双向绑定)
上一章介绍了AngularJS框架的插值语法和ng-bind指令,本章介绍ng-model指令,本指令用于实现input和变量的双向绑定。 示例代码: <!DOCTYPE html> <html ng-app> <head> <meta charset="utf-8"> <title>ng-model directive</title> </head> <body ng-controller="HelloController"> <div> <p>双向绑定</p> <input ng-model="greeting"> <p>Hello {{greeting || "World"}}</p> <button ng-click="init()">重置</button> <hr> </div> <script src="../lib/angularjs/1.2.26/angular.min.js"></script> <script> function HelloController($scope) { $scope.init = function() { $scope.greeting = "Hello"; } } </script> </body> </html> input加上ng-model指令后,框架会负责input和greeting变量的自动同步。 插值中支持表达式语法