gpt4 book ai didi

javascript - 为什么我收到错误 : $injector:unpr Unknown Provider?

转载 作者:行者123 更新时间:2023-11-29 17:58:34 25 4
gpt4 key购买 nike

我是 Angular 的新手,我正在尝试用工厂替换一些范围数据结构,但是依赖注入(inject)导致了一个问题。请帮忙

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 创建一个新的模块实例,因此销毁任何以前的实例。

关于javascript - 为什么我收到错误 : $injector:unpr Unknown Provider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014144/

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