zoukankan      html  css  js  c++  java
  • Angularjs中controller的三种写法

    在Angular中,Directive、Service、Filter、Controller都是以工厂方法的方式给出,而工厂方法的参数名对应着该工厂方法依赖的Service。angularjs中controller其实就是一个方法,它有三种写法,下面来一起看看吧。

    第一种:

    <pre name="code" class="javascript">var AppController = ['$scope', function($scope){
       $scope.notifyServiceOnChage = function(){
       console.log($scope.windowHeight);
     };
    }];
    app.controller('AppController',AppController);

    在定义AppController的时候,先声明方法需要注入的参数,然后再定义方法体。最后将AppController绑定到app上。

    第二种:

    app.controller('AppController'function($scope){ 
      $scope.notifyServiceOnChage = function(){
       console.log($scope.windowHeight);
     };
    })

    直接在app的controller属性定义,首先是controller名字,然后是方法体。

    第三种:

    function AppController($scope) {
      $scope.notifyServiceOnChage = function(){ 
       console.log($scope.windowHeight);
     };
    }

    直接写方法,然后在ng-controller引用该方法

    总结

    以上就是关于angularjs中controller三种写法的全部内容,不知道大家都学会了没有,希望这篇文章的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

  • 相关阅读:
    ASP.NET MVC 学习之路由(URL Routing)
    Linux 部署ASP.NET SQLite 应用 的坎坷之旅 附demo及源码
    linux解压zip文件
    /bin/sh^M:解释器错误:没有那个文件或目录
    mysql查看连接情况
    linux编译qt
    没有可用的软件包 xxx,但是它被其它的软件包引用了
    什么是人月
    qt linux 打包
    linux里安装使用svn
  • 原文地址:https://www.cnblogs.com/ifworld/p/8490528.html
Copyright © 2011-2022 走看看