Javascript 无法将控制器加载到我的angularjs模块中

Javascript 无法将控制器加载到我的angularjs模块中,javascript,angularjs,model-view-controller,angularjs-controller,Javascript,Angularjs,Model View Controller,Angularjs Controller,我试图将angularjs控制器拆分为文件,但每次都失败了 目录结构: --public-- --js/controller/resturant.js --js/master.js --index.php master.js的代码 angular.module("rsw",['rsw.controller']); resturant.js的代码 angular.module('rsw.controller').controller('r

我试图将angularjs控制器拆分为文件,但每次都失败了

目录结构:

--public--
        --js/controller/resturant.js
        --js/master.js
        --index.php
master.js的代码

angular.module("rsw",['rsw.controller']);
resturant.js的代码

    angular.module('rsw.controller').controller('resturant', ['$scope', '$http', function($scope, $http){
    $scope.data="Test"
}]);
index.php的代码

--
<div ng-app="rsw">
  <span ng-controller="resturant">
    {{ data }}
  </span>
</div>
--
--
{{data}}
--
编辑:
我在index.php中只包含了'master.js',我是否也需要导入'resturant.js?

您需要使用正确的调用。也就是说,
angular.module(名称)
检索模块并
angular.module(名称,[requires])
创建一个模块

创建模块后,您需要将其作为应用程序的依赖项:

angular.module("rsw",['rsw.controller']);
固定代码:

angular.module(“rsw”、['rsw.controller']);
角度模块('rsw.controller',[])
.controller('resturant',['$scope','$http',
函数($scope,$http){
$scope.data=“测试”
}
]);

{{data}}

您需要使用正确的调用。也就是说,
angular.module(名称)
检索模块并
angular.module(名称,[requires])
创建一个模块

创建模块后,您需要将其作为应用程序的依赖项:

angular.module("rsw",['rsw.controller']);
固定代码:

angular.module(“rsw”、['rsw.controller']);
角度模块('rsw.controller',[])
.controller('resturant',['$scope','$http',
函数($scope,$http){
$scope.data=“测试”
}
]);

{{data}}

我认为您的控制器设置错误。 请尝试键入以下内容:

angular.module('rsw').controller('resturant', ['$scope', '$http', function($scope, $http){
    $scope.data="Test"
 }]);

我想你把控制器设置错了。 请尝试键入以下内容:

angular.module('rsw').controller('resturant', ['$scope', '$http', function($scope, $http){
    $scope.data="Test"
 }]);

像这样的东西应该有用

文件结构:

--public
  --js
    --controllers
      --resturant.js
    --app.js
    --appRoutes.js
--views
  --index.php

// resturant.js
    angular.module('rswCtrl', []).controller('RswController', [$scope, function($scope) {

}]);

 // appRoutes.js

   angular.module('appRoutes', []).config(['$routeProvider','$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'views/index.php',
      controller: 'RswController' 
    });
  }]);

// app.js
angular.module('myApp', ['appRoutes', 'rswCtrl']);

然后别忘了在index.php文件中包含所有这些文件的路径。希望我没有遗漏一些东西。

类似的东西应该可以

文件结构:

--public
  --js
    --controllers
      --resturant.js
    --app.js
    --appRoutes.js
--views
  --index.php

// resturant.js
    angular.module('rswCtrl', []).controller('RswController', [$scope, function($scope) {

}]);

 // appRoutes.js

   angular.module('appRoutes', []).config(['$routeProvider','$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'views/index.php',
      controller: 'RswController' 
    });
  }]);

// app.js
angular.module('myApp', ['appRoutes', 'rswCtrl']);

然后别忘了在index.php文件中包含所有这些文件的路径。希望我没有遗漏什么。

我现在遇到了这个错误:
未捕获错误:[$injector:modulerr]
@JaskaranSinghPuri在我的答案中有一个小的输入错误,忘记了
'rsw.controller',[]
。这可能是原因吗?@JaskaranSinghPuri我添加了一个代码片段,其中只包含上面发布的我的更改,它可以工作。如果您有错误,请共享完整的错误+堆栈。这是一个文件代码,我想将我的控制器拆分为不同的文件!@JaskaranSinghPuri它将以完全相同的方式处理多个文件文件。如果你有两个标签,一个接一个,它将以相同的方式运行。很遗憾,你的错误无法修复,因为你没有提供必要的信息。我现在收到此错误:
Uncaught error:[$injector:modulerr]
@JaskaranSinghPuri在我的回答中有一个小的打字错误,忘记了
的rsw.controller',[]
。这可能是原因吗?@JaskaranSinghPuri我添加了一个代码片段,其中只包含上面发布的我的更改,它可以工作。如果您有错误,请共享完整的错误+堆栈。这是一个文件代码,我想将我的控制器拆分为不同的文件!@JaskaranSinghPuri它将以完全相同的方式处理多个文件文件。如果您有两个标签,它们将以相同的方式运行。很遗憾,由于您没有提供必要的信息,因此无法修复您的错误。您已经创建了一个模块“rsw”,并在另一个模块“rsw.controller”模块中创建了控制器。您是否将“rsw.controller”模块作为依赖项添加到“rsw”模块中?是,是直到不工作为止您已经创建了一个模块“rsw”,并在另一个模块“rsw.controller”中创建了控制器。您是否在“rsw”模块中添加了“rsw.controller”模块作为依赖项?是的,仍然不工作哦,是的,另一个输入错误问题,请尝试如下:
angular.module('rsw').controller('resturant',function($scope,$http){$scope.data=“Test”});
哦,是的,另一个输入错误的问题,尝试如下:
angular.module('rsw').controller('resturant',function($scope,$http){$scope.data=“Test”});