gpt4 book ai didi

javascript - Angular "ng:areq"错误,工厂问题

转载 作者:行者123 更新时间:2023-12-03 11:59:43 27 4
gpt4 key购买 nike

我已经设置了 Angular Factory 和 Controller ,以使用其 API 从 Instagram 中提取图像。我在尝试加载页面时遇到错误,但似乎无法找出问题所在。我的 html、应用程序、路由、工厂和 Controller 代码如下。我将测验 Controller 排除在外,因为它运行正常。我认为问题可能在于工厂,因为我刚开始使用它们。

index.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<base href="/">

<title>Starter Node and Angular</title>

<!-- CSS -->
<link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css"> <!-- custom styles -->

<!-- JS -->
<script src="libs/jquery/dist/jquery.min.js"></script>
<script src="libs/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="libs/angular/angular.min.js"></script>
<script src="libs/angular-route/angular-route.min.js"></script>
<script src="libs/angular-resource/angular-resource.min.js"></script>

<!-- ANGULAR CUSTOM -->
<script src="js/controllers/QuizCtrl.js"></script>
<script src="js/controllers/InstaCtrl.js"></script>
<script src="js/services/InstaFactory.js"></script>
<script src="js/appRoutes.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="app">

<!-- HEADER -->
<div class="navbar-wrapper">
<div class="container navvy">

<div class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Frank Social</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#">About Us</a></li>
<li><a href="#">Sign Up</a></li>
</ul>
</div>
</div>
</div>

</div>
</div>

<!-- ANGULAR DYNAMIC CONTENT -->
<div ng-view></div>

</body>
</html>

app.js:

angular.module('app', ['ngRoute', 'appRoutes', 'InstaFactory', 'QuizCtrl', 'InstaCtrl']);

appRoutes.js:

angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

$routeProvider

// home page
.when('/', {
templateUrl: 'views/home.html',
controller: 'QuizController'
})

// quiz page that will use the QuizController
.when('/quiz', {
templateUrl: 'views/quiz.html',
controller: 'QuizController'
})

// insta page that will use the InstaController
.when('/insta', {
templateUrl: 'views/insta.html',
controller: 'InstaController'
});

$locationProvider.html5Mode(true);

}]);

InstaCtrl.js:

angular.module('InstaCtrl', []).controller('InstaController', function($scope, Instagram) {

$scope.example1 = {
hash: 'angular'
};

$scope.example2 = {
hash: 'fit'
};

$scope.example3 = {
hash: 'food'
};

var instagramSuccess = function(scope, res) {
if (res.meta.code !== 200) {
scope.error = res.meta.error_type + ' | ' + res.meta.error_message;
return;
}
if (res.data.length > 0) {
scope.items = res.data;
} else {
scope.error = "This hashtag has returned no results";
}
};

Instagram.get(9, $scope.example1.hash).success(function(response) {
instagramSuccess($scope.example1, response);
});

Instagram.get(9, $scope.example2.hash).success(function(response) {
instagramSuccess($scope.example2, response);
});

Instagram.get(9, $scope.example3.hash).success(function(response) {
instagramSuccess($scope.example3, response);
});
});

InstaFactory.js:

angular.module('InstaFactory', []).factory('Instagram', function($http) {
var base = "https://api.instagram.com/v1";

var clientId = 'MY-CLIENT-ID-HERE';
return {
'get': function(count, hashtag) {
var request = '/tags/' + hashtag + '/media/recent';
var url = base + request;
var config = {
'params': {
'client_id': clientId,
'count': count,
'callback': 'JSON_CALLBACK'
}
};
return $http.jsonp(url, config);
}
};
});

最佳答案

您的应用程序结构似乎是错误的,因为模块用于打包可重用的代码,例如您正在定义一个新模块以在其中添加一个工厂刚刚将其添加到应用程序模块中您应该将模块视为主要模块函数并使用工厂或服务来实现特定行为,就像使用 Instagram 工厂一样。

关于javascript - Angular "ng:areq"错误,工厂问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com