单独的控制器AngularJS

时间:2016-07-31 15:23:48

标签: javascript angularjs

我是AngularJS的新手,正在尝试代码。我没有得到的是如何制作单独的模块。例如,我想拥有一个用于所有用户功能的模块,例如登录,注册,忘记密码等。

我找不到合适的教程来学习如何做到这一点。有人能帮助我吗?

我在 app.js

中有这个
// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('AppCtrl', function($scope, $http) {
  $scope.data = {};

  $scope.submit = function(){
    var link = 'http://nikola-breznjak.com/_testings/ionicPHP/api.php';

    $http.post(link, {username : $scope.data.username}).then(function (res){
      $scope.response = res.data;
    });
  };
});

这是我的 html

<body ng-app="starter" ng-controller="AppCtrl">
  <ion-pane>
      <ion-header-bar class="bar-stable">
          <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>

      <ion-content padding="true">
          <form ng-submit="submit()">
              <label class="item item-input item-stacked-label">
                  <span class="input-label">Username</span>
                  <input type="text" name="username" placeholder="enter username" ng-model="data.username">
              </label>

              <input class="button button-block button-positive" type="submit" name="submit" value="Submit to server">
          </form>

          <div class="card">
              <div class="item item-text-wrap">
                  Response: <b ng-bind="response"></b>
              </div>
          </div>
      </ion-content>
  </ion-pane>
  </body>

编辑(我认为应该是这样):

我的 app.js

angular.module('starter', ['ionic','login'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

我的 usersController.js

var app = angular.module("login", []);

app.controller('AppCtrl', function($scope, $http) {
    $scope.data = {};

    $scope.submit = function(){
        var link = 'http://nikola-breznjak.com/_testings/ionicPHP/api.php';

        $http.post(link, {username : $scope.data.username}).then(function (res){
            $scope.response = res.data;
        });
    };
});

我在html中包含 userController.js

1 个答案:

答案 0 :(得分:3)

您可以轻松地将代码模块化为:

Step1 :在主模块定义中将所有模块定义为依赖项

angular.module('starter', [
    'ionic',
    'starter.login',
    'starter.register',
    'starter.forgot_password',
    //and any other module you want to add
])

Step2:分别定义这些模块

//note these can be in same files or separate files all together

angular.module('starter.login',[
     'starter.login.services', //sub module for services
     'starter.login.directives', //sub module for directives
  ]);
angular.module('starter.register',[]);
angular.module('starter.forgot_password',[]);

//you can further create submodules for above modules
//eg: sub module for directive, sub module for service etc as

步骤3:然后,您可以单独为这些模块定义控制器/服务/工厂/指令。

//giving example for just login controller
 angular.module('starter.login').controller(function($scope){
      //note this module must be defined first before using it with a controller. so files must be loaded in the right order
});

   //similarly you will have to define your sub-modules before using them with services/controllers or directives

有关详细信息,您可以阅读这篇写得非常好的博客: https://www.safaribooksonline.com/blog/2014/03/27/13-step-guide-angularjs-modularization/

并遵循John Papa关于构建目录的指南: https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#modules