为什么我得到错误:$injector:unp未知提供程序

Why am i getting Error: $injector:unpr Unknown Provider?

本文关键字:unp 未知 程序 injector 错误 为什么      更新时间:2023-09-26

我对angular还很陌生,我正试图用工厂替换一些范围数据结构,但依赖项注入导致了问题。请帮助

https://jsfiddle.net/bennacer860/k8js1o4m/1/

angular.module('flapperNews', [])
  .factory('posts', [function() {
    var o = {
      posts: []
    };
    return o;
  }]);

angular.module('flapperNews', [])
  .service('myService', function() { /* ... */ })
  .controller('MyController', ['myService', function(myService) {
    // Do something with myService
    console.log("test");
  }]);

angular.module('flapperNews', [])
  .controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
    $scope.test = 'Hello world!';
    // $scope.posts = [
    //   {title: 'post 1', upvotes: 5},
    //   {title: 'post 2', upvotes: 2},
    //   {title: 'post 3', upvotes: 15},
    //   {title: 'post 4', upvotes: 9},
    //   {title: 'post 5', upvotes: 4}
    // ];
    $scope.posts = posts.posts;
    $scope.addPost = function() {
      if (!$scope.title || $scope.title === '') {
        return;
      }
      $scope.posts.push({
        title: $scope.title,
        link: $scope.link,
        upvotes: 0
      });
      $scope.title = '';
      $scope.link = '';
    };
    $scope.incrementUpvotes = function(post) {
      post.upvotes += 1;
    };
  }]);

消除每个angular.module('flapperNews', [])中第一个之后的[]

在那里传递一个参数告诉angular创建模块的一个新实例,从而销毁任何以前的实例。