Javascript 角度1.2我在哪里声明错误?工厂未定义
请原谅,如果这是一个重复,我已经尝试搜索其他线程,但我没有看到类似的注释 错误:未定义factoryGetJavascript 角度1.2我在哪里声明错误?工厂未定义,javascript,angularjs,Javascript,Angularjs,请原谅,如果这是一个重复,我已经尝试搜索其他线程,但我没有看到类似的注释 错误:未定义factoryGet var myapp = angular.module('myapp', ['ngRoute','myServices']); /* adds in the myServices module and its factory as a dependency */ myapp.controller( 'mycontroller', [ '$scope',
var myapp = angular.module('myapp', ['ngRoute','myServices']);
/* adds in the myServices module and its factory as a dependency */
myapp.controller(
'mycontroller',
[
'$scope',
function($scope) {
$scope.mydata = factoryGet.query();
}
]
);
myapp.config(specifyRoute);
function specifyRoute ($routeProvider) {
$routeProvider
.when('/', {template: '<div>no template</div>'})
.when('/pagea', {template: '<div>Page A</div>'})
.when('/pageb', {template: '<div>Page B</div>'})
.when('/pagec', {template: '<div>Page C</div>'});
}
// define a new module called myServices
angular.module('myServices', ['ngResource'])
.factory('factoryGet', function ($resource) {
return $resource(
'/airports', // server path
{ method: 'getTask', q: '*' }, // Query parameters
{'query': { method: 'GET' }}
);
});
var myapp=angular.module('myapp',['ngRoute','myServices']);
/*作为依赖项添加到myServices模块及其工厂*/
myapp.controller(
“我的控制器”,
[
“$scope”,
函数($scope){
$scope.mydata=factoryGet.query();
}
]
);
myapp.config(specifyrute);
函数SpecificRoute($routeProvider){
$routeProvider
.when(“/”,{template:'no template'})
.when('/pagea',{template:'Page A'})
.when('/pageb',{template:'Page B'})
.when('/pagec',{template:'Page C'});
}
//定义一个名为myServices的新模块
angular.module('myServices',['ngResource']))
.factory('factoryGet',函数($resource){
返回$resource(
“/airports”,//服务器路径
{method:'getTask',q:'*'},//查询参数
{'query':{method'GET'}
);
});
html是相当标准的
<div class="container">
<a href="#" class="btn btn-default">Home</a>
<a href="#/pagea" class="btn btn-default">Page A</a>
<a href="#/pageb" class="btn btn-default">Page B</a>
<a href="#/pagec" class="btn btn-default">Page C</a>
</div>
<div class="container" ng-controller="mycontroller">
<div ng-view></div>
</div>
试试:
成功了!谢谢顺便说一句,我也不需要在myapp之前移动我的服务。可能是因为它声明为myappI的依赖项factoryGet必须声明两次,这是因为控制器函数在依赖项数组中吗?@PrimeLens:这是内联数组注释,以避免在缩小JS时出现问题,因为字符串没有缩小。正确的!非常感谢你的帮助。如果我可以的话,还有一个问题是哪种类型的关系。我注意到,如果json返回从对象到数组的更改,我返回到一个错误。如果我需要允许它是其中之一,该怎么办?@PrimeLens:在声明$resource时使用
isArray:false
或isArray:true
,具体取决于它是对象还是数组。
myapp.controller(
'mycontroller',
[
'$scope',
'factoryGet', //declare your dependency
function($scope,factoryGet) { //declare your dependency
$scope.mydata = factoryGet.query();
}
]
);