angular1控制器主要作用(大致思路)

初始化module数据(为应用中的模型设置初始状态)
用过$scope对象把数据模型或函数行为暴露给视图
监视模型的变化,做出相应动作

 <!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<table ng-controller="muController">
    <tr>
        <td>用户名:</td>
        <td><input type="text" ng-model="user.username"/></td>
    </tr>
    <tr>
        <td>密码:</td>
        <td><input type="password" ng-model="user.password"/></td>
    </tr>
    <tr>
        <td><input type="button" value="登陆" ng-click="login()"/></td>
    </tr>
</table>
<script src="node_modules/angular/angular.js"></script>
<script>
    var app = angular.module("myApp", []);
    app.controller("muController", ["$scope", function ($scope) {
        $scope.user = {
            username: '',
            password: ''
        }
        $scope.login = function () {
            console.log($scope.user);
        }
    }])
</script>
</body>
</html>

标签: angular, angular.js, angular控制器

添加新评论