发布于 2017-06-24 22:14:49 | 161 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的AngularJS开发指南,程序狗速度看过来!

AngularJS 前端JS框架

AngularJS诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入,等等。


这篇文章主要介绍了AngularJS 模块化,这里整理了详细的资料及简单实例代码,实现效果图,有需要的小伙伴可以参考下

AngularJS有几大特性,比如:

  1 MVC

  2 模块化

  3 指令系统

  4 双向数据绑定

那么本篇就来看看AngularJS的模块化。

  首先先说一下为什么要实现模块化:

  1 增加了模块的可重用性

  2 通过定义模块,实现加载顺序的自定义

  3 在单元测试中,不必加载所有的内容

  之前做的几个例子,控制器的代码直接写在script标签里面,这样声明的函数都是全局的,显然不是一个最好的选择。

  下面看看如何进行模块化:       


 <script type="text/javascript">
      var myAppModule = angular.module('myApp',[]);
      
      myAppModule.filter('test',function(){
        return function(name){
          return 'hello, '+name+'!';
        };
      });

      myAppModule.controller('myAppCtrl',['$scope',function($scope){
        $scope.name='xingoo';
      }]);
    </script>

  首先,通过全局变量angular创建模块myAppModule

angular.module('myApp',[]);

  第一个参数是绑定的应用app名称,这个app标识了页面中angular的入口点,类似main函数的作用。

  第二个参数[]里面标识了依赖的模块。

  下面看看如何使用模块吧!


<!doctype html>
<html ng-app="myApp">
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
  </head>
  <body>
    <div ng-controller="myAppCtrl">
      {{name | test }}
    </div>
    <script type="text/javascript">
      var myAppModule = angular.module('myApp',[]);
      
      myAppModule.filter('test',function(){
        return function(name){
          return 'hello, '+name+'!';
        };
      });

      myAppModule.controller('myAppCtrl',['$scope',function($scope){
        $scope.name='xingoo';
      }]);
    </script>
  </body>
</html>

  直接绑定myApp到ng-app上,就可以了。

  在script中,我们通过模块创建了一个filter和一个控制器。

  filter的作用是 添加字符串修饰。

  控制器的作用则是初始化变量。

  程序的运行结果如下:

          以上就是对AngularJS 模块化 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务