javascript - 单独的 Controller AngularJS

标签 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 步:在主模块定义中将所有模块定义为依赖项

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

第二步:分别定义这些模块

//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

第三步:然后您可以为这些模块单独定义 Controller /服务/工厂/指令。

//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

关于javascript - 单独的 Controller AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685475/

相关文章:

javascript - 如何避免在 Angular 1.2 中使用 'scope.$parent...'

angularjs - 防止列数破坏内部元素

javascript - 存在于另一个数组中的过滤器选项

javascript - (JQuery-UI)有没有办法检测在 Close() 处理程序之前在对话框中按下了什么按钮?

javascript - 如何在文本选择中获取所有 <img> 标签?

javascript - vue中如何使用download属性?

javascript - 如何使用 AngularJS 获得 promise 结果

javascript - JavaScript 中原型(prototype)链的结尾是什么——null 或 Object.prototype?

javascript - 从 JSON 调用对象的值时,为什么必须用引号和加号括起来?

javascript - 选择文件后范围变量未更改其值