我是棱角分明的新手。我试图在我的控制器和工厂之间建立依赖关系。 控制器:
var app = angular.module('app', ['ngRoute']);
app.controller('MainViewController',['MainViewFactory', function ($scope, MainViewFactory ,$location) {
//codes here
}]);
这是工厂:
var app = angular.module('app');
app.factory('MainViewFactory' , function () {
var factory = {};
//factory codes here
return factory;
});
但在加载页面时我有喷射器错误
未捕捉错误:[$ injector:nomod]模块' app'不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"/>
<title></title>
<link href="/public/css/bootstrap.min.css" rel="stylesheet">
<link href="/public/css/font-awesome.min.css" rel="stylesheet"/>
<link href="/public/css/style.css" rel="stylesheet"/>
<link href="/public/css/style-blue1.css" rel="stylesheet" id="style_color"/>
<link href="/public/css/jstree-themes/default/style.min.css" rel="stylesheet">
<link href="/public/lib/toaster/toaster.css" rel="stylesheet">
</head>
<body ng-app="app" ng-controller="MainViewController" >
<div id="navbar">
<span> </span>
</div>
<div id="container">
<div id="tools">
<button class="btn" ng-click="functions()"></button>
<button class="btn" ng-click="setting()"></button>
<button class="btn" ng-click="guide()"></button>
<button class="btn" ng-click="contactUs()"></button>
</div>
<div id="content" ng-view=""> </div>
</div>
<script type="text/javascript" src="/public/lib/angular/angular.js"></script>
<script type="text/javascript" src="/public/lib/angular/angular-route.min.js"></script>
<script type="text/javascript" src="/public/lib/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/public/app/factories/MainViewFactory.js"></script>
<script type="text/javascript" src="/public/app/controllers/MainViewController.js"></script>
</body>
</html>
帮助我!
答案 0 :(得分:1)
<强>更新强>
您应该将控制器放在工厂脚本之前。
<script type="text/javascript" src="/public/app/controllers/MainViewController.js"></script>
<script type="text/javascript" src="/public/app/factories/MainViewFactory.js"></script>
您不必再为工厂申报相同的模块,您可以这样定义,
var app = angular.module('app', ['ngRoute']);
app.controller('MainViewController',['$scope','MainViewFactory','$location', function ($scope, MainViewFactory ,$location) {
}]);
app.factory('MainViewFactory' , function () {
var factory = {};
//factory codes here
return factory;
});
这是工作Plunker