angularJS(1)

模块
浏览器

在使用angularJS编写代码的时候,咱们经常把不一样功能分为不一样的模块进行编写,这样能更好的进行分工合做,提升工做效率。

首先 //定义主模块

    angular.module("mainApp",["mainApp.Contrl","mainApp.filter"]);

    其中前面参数是主模块名字,后面中括号里是子模块的名字。

    angular.module("mainApp.Contrl",[])

           .controller("maincontrl",function($scope){

               $scope.ary=[14,24,58,44,51,67];

        });

    上面是随便列举了一个控制器的子模块。若是要继续添加另外的控制器模块,只需在后面继续加,以下代码

    angular.module("mainApp.Contrl",[])

           .controller("maincontrl",function($scope){

               $scope.ary=[14,24,58,44,51,67];

        }).controller("maincontrl1",function($scope){

               $scope.ary1=[14,24,58,44,51,67];

        });

    若是是定义一个另外的模块,须要新建模块。

    angular.module("mainApp.filter",[])

          .filter("orderByFilter",function(){

            alter("1");

    });

    这些新建的模块都必须将名字添加到主模块中。


指令

基于咱们对HTML元素的理解,指令本质上就是angularJS扩展具备自定义功能的HTML元素的途径。咱们能够经过代码自定义一个元素,它能够做为正常的标签使用,并能在因此浏览器中工做。

    angular.module("mainApp.directive",[])

           .directive('say',function(){

               return {

                   restrict:"E",

                   template:"<span>hellow world!</span>",

                   replace:true

              }

    })

    如上代码,自定义了一个‘say’元素。
spa

相关文章
相关标签/搜索