<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title></title> </head> <body ng-init="thing={name:'world'}"> <h1>Hello,{{thing.name}}</h1> <div ng-controller="HelloCtrl"> Say hello to:<input type="text" ng-model="thing.name"> <h1>Hello,{{thing.name}}!!</h1> </div> </body> <script type="text/javascript" src="https://www.jb51.net/js/angular.js" ></script> <script type="text/javascript" src="https://www.jb51.net/js/controller.js" ></script> </html>
var app = angular.module("myApp",[]) app.controller("HelloCtrl", function($scope) { $scope.name = "youyi"; });
结果:
改变表单中的值结果如下:
避免直接绑定变量给作用域属性,对象属性的双向数据绑定是最好的解决方案。