无法将控制器加载到我的 angularjs 模块中
Unable to get controller loaded into my angularjs module
我正在尝试将我的 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>
--
编辑:
我的 index.php 中只包含了 'master.js',我还需要导入“resturant.js”吗?
您需要使用正确的 module definition 调用。也就是说,angular.module(name)
检索 一个模块并且 angular.module(name, [requires])
创建 一个。
angular.module('rsw.controller', [])
.controller('resturant', ['$scope', '$http', function($scope, $http){
$scope.data="Test"
}]);
创建模块后,您需要将其作为应用程序的依赖项:
angular.module("rsw",['rsw.controller']);
固定码:
angular.module("rsw", ['rsw.controller']);
angular.module('rsw.controller', [])
.controller('resturant', ['$scope', '$http',
function($scope, $http) {
$scope.data = "Test"
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="rsw">
<span ng-controller="resturant">
{{ data }}
</span>
</div>
我认为您的控制器设置有误。
请尝试输入以下内容:
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 文件中包含所有这些文件的路径。希望我没有遗漏什么..
我正在尝试将我的 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>
--
编辑: 我的 index.php 中只包含了 'master.js',我还需要导入“resturant.js”吗?
您需要使用正确的 module definition 调用。也就是说,angular.module(name)
检索 一个模块并且 angular.module(name, [requires])
创建 一个。
angular.module('rsw.controller', [])
.controller('resturant', ['$scope', '$http', function($scope, $http){
$scope.data="Test"
}]);
创建模块后,您需要将其作为应用程序的依赖项:
angular.module("rsw",['rsw.controller']);
固定码:
angular.module("rsw", ['rsw.controller']);
angular.module('rsw.controller', [])
.controller('resturant', ['$scope', '$http',
function($scope, $http) {
$scope.data = "Test"
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="rsw">
<span ng-controller="resturant">
{{ data }}
</span>
</div>
我认为您的控制器设置有误。 请尝试输入以下内容:
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 文件中包含所有这些文件的路径。希望我没有遗漏什么..