【发布时间】:2017-01-13 18:35:54
【问题描述】:
我正在使用 JHispter,我看到它使用了这些 AngularJS 规则:https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md
使用 IIFE、Getters、Use Strict、Named Functions、ControllerAs 等,我想创建一个简单的页面来解析 JSON 并显示电影列表(标题、导演、时长)和持续时间更长的页面。 我已经搜索并尝试了一整天,但没有任何效果。工厂不能在控制器中使用,我使用 $inject 注入它。
这是我的 index.html
<html>
<head>
<title>Hello Angular</title>
<link href="stile.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="">
</head>
<body ng-app="myApp">
<h1>Hello Angular</h1>
<div ng-controller="myController as sc">
<h1>angular JSON test</h1>
<!-- <p>Print movie list</p>
<ul >
<li ng-repeat="film in sc.elencoFilm">
{{film.title}}, {{film.director}}, {{film.time}}
</li>
</ul>
<p >Trova il film più lungo: {{sc.maxTimeFilm().title}} </p> -->
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="JS/app.config.js"></script>
<script src="JS/app.state.js"></script>
<script src="JS/app.service.js"></script>
<script src="JS/app.controller.js"></script>
</body>
</html>
我的 app.config.js
(function() {
'use strict';
angular
.module("myApp", []) ;
})();
我的 app.state.js
(function() {
'use strict';
angular
.module('myApp')
.config(stateConfig);
stateConfig.$inject = ['$routeProvider'];
function stateConfig($routeProvider) {
$routeProvider.when('/', {
templateUrl:"index.html",
controller:"serverController"
});
}
})();
我的 app.controller.js
(function () {
'use strict';
angular
.module("myApp",[])
.controller("myController", myController);
//myController.$inject = ['$scope', '$http'];
myController.$inject = ['$scope', '$http','myFactory'];
function myController($scope, $http, myFactory) {
//function myController($scope, $http){//, myFactory) {
var vm = this;
var elencoFilm={};
myFactory.getMovies().then(function (response) {
vm.elencoFilm = response;
});
vm.maxTimeFilm = getMaxTimeFilm();
function getMaxTimeFilm() { //return the longest film
}
}
})();
我的 app.service.js
(function () {
'use strict';
angular.module('myApp',[])
.factory('myFactory', myFactory);
myFactory.$inject = ['$scope', '$http','myFactory'];
function myFactory($scope, $http) {
console.log("sono nella factory");
return {
getMovies: function ($http) {
return $http.get('https://api.myjson.com/bins/1bgtg3');
/* .then(function (response) {
return response.data.movies;
});*/
}
}
}
})();
它总是返回这个错误: https://docs.angularjs.org/error/$injector/unpr?p0=myFactoryProvider%20%3C-%20myFactory%20%3C-%20myController
无法将 myFactory 识别为 myController 函数!
在 app.controller.js 这一行
function myController($scope, $http, myFactory) { 这打破了错误!
谢谢你的帮助!! :)
【问题讨论】:
-
似乎您将 myFactory 作为 myFactory 的依赖项
-
尝试从
$inject中删除'myFactory'以获得函数myFactory() -
从工厂移除 $scope。
-
嗨,谢谢!我已经从 app.service.js @Alexander-Staroselsky 中删除了 myFactory 注入它总是显示相同的错误 (angular.js:11607 Error: [$injector:unpr])
-
我已经从 app.service.js 中删除了 myFactory 注入:@AlexanderStaroselsky myFactory.$inject = ['$scope','$http']; function myFactory($scope, $http) { 它总是向我显示相同的错误:(angular.js:11607 Error: [$injector:unpr])
标签: javascript angularjs jhipster