ng-init为变量设置初始值,只要在ng-app范围内,对任何一其定义的变量均有效;其方式为ng-init="变量名称=‘变量值’";注意外边双引号,初始值要用单引号:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="angular.js"></script> </head> <body > <div ng-app=""ng-init="gender='male'"> <h1>ng-init="arr='xxx'"初始值定义</h1> <h2>{{gender}}</h2> </div> </body> </html>
显示效果为下图: