Javascript 使用ngTagsInput创建复杂服务
我正在用我的博客实现,这样当添加或编辑新帖子时,用户可以向其添加现有的或他们自己的标签 我的博客使用firebase数据源,可通过工厂访问:Javascript 使用ngTagsInput创建复杂服务,javascript,angularjs,angular-services,ng-tags-input,Javascript,Angularjs,Angular Services,Ng Tags Input,我正在用我的博客实现,这样当添加或编辑新帖子时,用户可以向其添加现有的或他们自己的标签 我的博客使用firebase数据源,可通过工厂访问: servicesModule.factory("postsDB", function($resource){ return $resource("https://datasource.firebaseio.com/Posts.json", { id: "@id" }, { update: {
servicesModule.factory("postsDB", function($resource){
return $resource("https://datasource.firebaseio.com/Posts.json", {
id: "@id"
},
{
update: {
method: "PUT"
},
query: {
method: 'GET',
isArray: false }
});
});
因为ngTagsInput函数需要在其他控制器中使用,所以我想将它变成一个服务,因为标签字段在订单中的引用方式不同。HTML如下所示:
<tags-input ng-model="post.Tags">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
在我的控制器中:
controllersModule.controller('AddPostCtrl', ["$scope", '$location', '$firebaseArray', '$sce', 'postsDB', 'AddTags', function($scope, $location, $firebaseArray , $sce, postsDB, AddTags){
AddTags(function(myTags){
$scope.post.Tags = myTags;
});
但是,我得到以下错误:
错误:[$injector:unpr]未知提供程序:$scopeProvider$scope
在工厂中不可用。在工厂中使用$scope是没有意义的。从Angular文档中可以看出:范围是应用程序控制器和视图之间的粘合剂。
很明显,您甚至没有在工厂中使用$Scope对象。因此,只要将其从工厂构造函数函数依赖项中删除,就可以了。这可能是因为$scope在服务和工厂中不能作为直接注入使用。那么如何将一个注入到另一个中呢?请参阅我上面的编辑。我已经删除了$scope
,但是现在我得到了以下错误:“TypeError:postsDB.child不是一个函数”。我正在尝试访问Posts>Post的子“标签”。它应该返回一个JSON数组“Tags”:[tag,tag1,tag2]。我这样做对吗?
controllersModule.controller('AddPostCtrl', ["$scope", '$location', '$firebaseArray', '$sce', 'postsDB', 'AddTags', function($scope, $location, $firebaseArray , $sce, postsDB, AddTags){
AddTags(function(myTags){
$scope.post.Tags = myTags;
});
ervicesModule.factory ( 'InputTags' , function($http, postsDB, $firebaseArray) {
var myTags = '';
var myTags = $firebaseArray(postsDB.child('Tags'));
function loadTags (query) {
return $http.get('/tags?query=' + query);
};
});