AngularJS中未定义Require

时间:2018-03-20 09:48:34

标签: javascript angularjs require

我有两个模块,即'用户'和'组',并且在两个模块中都有不同的路由。现在我想在其他模块中使用它们,我试图要求两个模块,但是错误为required is not defined。我该如何解决? 这是我的代码:

appGroup.js

    let myNinjaAppforGroup = angular.module('myNinjaAppforGroup',['ngRoute']);
    //injected ngRoute module as a dependency in the above statement

    myNinjaAppforGroup.config(['$routeProvider',function($routeProvider)
    {
    //code executes before application runs

    $routeProvider
      .when('/group/home',{
        templateUrl:'views/home.html',  //which view to be rendered if user visits this url
      })
      .when('/group/directory',{
        templateUrl:'views/directory.html',
        controller:'NinjaController'//it will be the controller for the mentioned route
      }).otherwise({
        redirectTo:'/group/home'
      });
    }]);

    myNinjaAppforGroup.run(function()
    {
    //code executes when application runs
    });

    myNinjaAppforGroup.controller('NinjaController',['$scope','$http',function($scope,$http){ 
    //scope is the glue between controller and view. Its also a dependency
      $scope.message="Hey Angular!";//this is accessable in views
      $scope.removeNinja = function(ninja)
      {
        let removedNinja = $scope.ninjas.indexOf(ninja);
        $scope.ninjas.splice(removedNinja,1);
      }
    $scope.addNinja = function()
    {
      $scope.ninjas.push({
        name:$scope.newNinja.name,
        rate:parseInt($scope.newNinja.rate),
        belt:$scope.newNinja.belt,
        available:true
      });
      $scope.newNinja.name="";
      $scope.newNinja.rate="";
      $scope.newNinja.belt="";
    }

    $http.get('model/ninjas.json').then(function(response){
      $scope.ninjas=response.data;
      //console.log(response); for checking the object received
      //whatever data we are getting from the http service is being saved here.
    })

    }]);

    module.exports = myNinjaAppforGroup;

`and appUsers.js`

let myNinjaAppforUsers = angular.module('myNinjaAppforUsers',['ngRoute']);
//injected ngRoute module as a dependency in the above statement

myNinjaAppforUsers.config(['$routeProvider',function($routeProvider)
{
//code executes before application runs

$routeProvider
  .when('/user/home',{
    templateUrl:'views/home.html',  //which view to be rendered if user visits this url
  })
  .when('/user/directory',{
    templateUrl:'views/directory.html',
    controller:'NinjaController'//it will be the controller for the mentioned route
  }).otherwise({
    redirectTo:'/user/home'
  });
}]);

myNinjaAppforUsers.run(function()
{
//code executes when application runs
});

myNinjaAppforUsers.controller('NinjaController',['$scope','$http',function($scope,$http){ 
//scope is the glue between controller and view. Its also a dependency
  $scope.message="Hey Angular!";//this is accessable in views
  $scope.removeNinja = function(ninja)
  {
    let removedNinja = $scope.ninjas.indexOf(ninja);
    $scope.ninjas.splice(removedNinja,1);
  }
$scope.addNinja = function()
{
  $scope.ninjas.push({
    name:$scope.newNinja.name,
    rate:parseInt($scope.newNinja.rate),
    belt:$scope.newNinja.belt,
    available:true
  });
  $scope.newNinja.name="";
  $scope.newNinja.rate="";
  $scope.newNinja.belt="";
}

$http.get('model/ninjas.json').then(function(response){
  $scope.ninjas=response.data;
  //console.log(response); for checking the object received
  //whatever data we are getting from the http service is being saved here.
})

}]);

module.exports = myNinjaAppforUsers;

现在我有另一个app.js文件,我想在那里要求这两个文件,怎么办呢?

1 个答案:

答案 0 :(得分:3)

要求在浏览器中不起作用。基本上需要一个node_module,通过它我们可以访问其他模块或文件。所以如果你在浏览器端使用它,请尝试其他东西,如import或self.import或者注入。< / p>

Doc:http://requirejs.org/docs/download.html

将此添加到您的项目中:http://requirejs.org/docs/release/2.2.0/minified/require.js

并查看此http://requirejs.org/docs/api.html