Angularjs中的Controller

概念一个应用(APP,本身也是一个大模块)是由若干个模块(module)组成的,每个模块实现一个功能。利于代码的复用。

书写格式:

<!DOCTYPE html>
<html ng-app="test"> <!---依赖--->
  <head>
    <meta charset="utf-8">
    <title></title>
        <!-- 1.引入angular包 -->
    <script src="angular.js" charset="utf-8"></script>
    <script>
     //2.AngularJS对外暴露了一个全局对象angular,调用他的方法创建模块
   //参数1:模块名称;参数2:此模块依赖的其他模块
    let mod=angular.module('test', []);
        
        // 3.用模块对象的方法去创建控制器
    mod.controller('main', function ($scope){        
        
      // 4.$scope为模型对象,将数据绑定到他身上
      $scope.a=0;
      $scope.b=0;
$scope.
int=function (str){ return parseInt(str); }; }); </script> </head> <!-- 5.将控制器绑定到视图上,这个body下就可以使用该控制器的数据 --> <body ng-controller="main"> <input type="text" ng-model="a"> <input type="text" ng-model="b"> {{int(a)+int(b)}} </body> </html>

controller——逻辑核心、大段代码

$scope——angular的作用域(所有的Angular里面的东西,都在$scope上)

依赖注入(依赖反转)
要什么参数,就能得到什么参数 例如:$scope, $http

猜你喜欢

转载自www.cnblogs.com/tianranhui/p/9333218.html
今日推荐