繁体   English   中英

角度“ ng:areq”错误,工厂问题

[英]Angular “ng:areq” error, factory issue

我已经建立了一个Angular Factory和Controller,以使用其API从Instagram提取图像。 尝试加载页面时出现错误,但似乎无法解决问题。 我的HTML,应用,路线,工厂和控制器代码如下。 由于功能正常,我省略了测验控制器。 我认为问题可能出在工厂,因为我是新手。

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工厂一样。

发现错误。 我将路由逻辑移到了appRoutes.js文件中,但是忘记了从我的部分.html文件中删除对控制器的引用。 一旦我从html中删除了此ng-controller引用,一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM