我使用factory如下:
var appModule = angular.module('appModule', ['ngRoute','restangular']);
appModule
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'home.html',
controller: 'ngHomeControl'
})
.when('/contacts', {
templateUrl: 'contacts.html',
controller: 'ngContactControl'
});
}]);
appModule
.factory('testFactory', function testFactory(Restangular) {
return {
getFriendList: function() {
return Restangular
.all('api/users/getfriendsinvitations/')
.getList()
.then(function(response) {
//
});
}
}
});
appModule
.controller('ngHomeControl', function($scope, testFactory) {
$scope.homeFriends = testFactory.getFriendList();
});
appModule
.controller('ngContactControl', function($scope, testFactory) {
$scope.contactFriends = testFactory.getFriendList();
});
Run Code Online (Sandbox Code Playgroud)
在这里,我不能获得的价值$scope.homeFriends和$scope.contactFriends从Restangular电话.任何人都可以建议我如何Restangular使用factory/ 来调用值service …
scope angularjs angularjs-service angularjs-factory restangular
我尝试http.get在angularjs服务中使用promise,对获得的集合进行一些操作,最后将其返回给控制器......
我的问题是如何$http.get()在服务中使用a 来获取和操作结果,然后将其返回给控制器,如下面的代码所示:
PEN代码
var app = angular.module('myApp', []);
app.controller('customersCtrl', ['$scope','customer',function($scope, customer) {
$scope.odds = customer.odds;
}]);
app.factory('customer', ['$http', function($http) {
var all = [{'Id':88, 'Name':"A"}, {'Id':89, 'Name':"ShoutNotBeHere"}];
var odds = [];
$http.get("http://www.w3schools.com/angular/customers.php")
.then(function(response) {
all = response.records;
});
angular.forEach(all, function(c, i) {
if (i % 2 == 1) {
odds.push(c);
}
});
return {odds: odds};
}]);Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
Odd ids from www.w3schools.com/angular/customers.php
<ul>
<li ng-repeat="c in odds">
{{ c.Id …Run Code Online (Sandbox Code Playgroud)javascript angularjs angularjs-factory angularjs-http angular-promise
我是角度js的新手,我正在尝试用分页构建一个简单的网格.我有一个工厂对象Entities,如下所示:
//app module
angular.module('project',['ngRoute','controllers', 'services'])
.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/',{
controller:'ListCtrl',
templateUrl:'list.html'
})
.when('/edit/:id',{
controller:'EditCtrl',
templateUrl:'form.html'
})
.otherwise({
redirectTo:'/'
})
}]);
//controller
angular.module('controllers',[])
.controller('ListCtrl',['$scope','Entities',function($scope ,Entities){
Entities.get(function(data){
$scope.entityCaption='Projects';
$scope.entities=data;
});
}])
// services module
angular.module('services', [])
.value('dataConfigs',
{
fetchUrl:'../fetch.php',
saveUrl:'../save.php',
entityName:'projects'
}
)
.factory('Entities', ['$http','$filter','dataConfigs', function($http,$filter,dataConfigs){
return{
pageNo:1,
rows:2,
sortBy:'id',
sortOrder:'desc',
get: function(callback){
$http({
url: dataConfigs.fetchUrl + '?method=grid&table='+dataConfigs.entityName+'&page='+this.pageNo+'&rows='+this.rows+'&sidx='+this.sortBy+'&sord='+this.sortOrder+'&format=assoc',
method: "POST"
}).success(function (data) {
callback(data);
});
},
getById:function(id,callback){
$http({
url: dataConfigs.fetchUrl + '?method=grid&table='+dataConfigs.entityName+'&where_id='+id+'&page=1&rows=1&sidx=id&sord=asc&format=assoc',
method: "POST"
}).success(function (data) {
if(data.records==1)
callback(data.rows[0]);
else …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的例子
(也可以在这个小提琴上找到:http://jsfiddle.net/depov5w6/)
我有2个工厂,一个返回一个具有属性'data'的对象(绑定到控制器的范围),另一个工厂有两个方法:populateData和pushData.populateData将dataSource.data的值设置为新值.pushData将一个新值推送到dataSource.data
angular.module("app",[])
.factory('dataSource', function() {
return {
data: ["Init Data"]
};
})
.factory('dataModifier', function(dataSource) {
return {
populateData: function() {
dataSource.data = ["data from populateData"];
},
pushData: function() {
dataSource.data.push("new data from push data");
}
}
})
.controller('dataCtrl', function($scope, dataSource, dataModifier) {
$scope.data = dataSource.data;
$scope.populateData = dataModifier.populateData;
$scope.pushData = dataModifier.pushData;
})
Run Code Online (Sandbox Code Playgroud)
在我看来,我有一个调用pushData的按钮和另一个调用populateData的按钮.我还显示$ scope.data
<div ng-app="app">
<div ng-controller="dataCtrl" class="container">
<p>Data: {{data}}</p>
<button ng-click="pushData()">Push Data</button>
<button ng-click="populateData()">Set Data To New Value</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果您查看小提琴,如果单击"推送数据"按钮,新值将被推送到阵列上.但是,如果单击"将数据设置为新值",则不会发生任何事情,并且后续单击以推送数据不会执行任何操作.我想我明白,当你点击填充数据时,你会丢失对$ scope.data绑定的数组的引用,但为什么$ …
我有一个运行k2的joomla网站,并通过在字符串末尾添加format = json来从中提取信息:https://www.example.com/posts? format = json ,输出如下内容:
{
site: {
url: "https://www.example.com",
name: "mySite"
},
category: {
id: "67",
name: "Gauteng",
alias: "gauteng",
link: "/tna/provincial/gauteng.html",
parent: "66",
extraFieldsGroup: "0",
image: null,
ordering: "1",
events: {
K2CategoryDisplay: ""
},
chidlren: []
},
items: [{
id="1",
title="The Title",
body="body content here..."
},
{
id="2",
title="The Title",
body="body content here..."
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在为此创建一个服务,只想访问"项目".
.factory('Posts', function($http) {
var posts = [];
return {
getPosts: function(){
return $http.get("https://www.example.com/posts?format=json").then(function(response){
posts = response;
return …Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-factory angularjs-http ionic-framework
我正在构建一个angularjs应用程序,它有各种控制器.对于两个控制器,我依赖注入相同的服务.是为每个注入它的控制器创建的服务的单独实例吗?
调用此$ http请求后(带server.refresh();)
MinecraftServer.prototype.refresh = function(){
return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData);
}
Run Code Online (Sandbox Code Playgroud)
这个函数this是window对象,而不是MinecraftServer对象:
MinecraftServer.prototype.acceptData = function(data){
data = data.data
if(data && data.online){
this.online = data.online;
//do more stuff
} else { // do more stuff }
}
Run Code Online (Sandbox Code Playgroud)
因此,不是让MinecraftServer对象更新它的属性,而是window获取属性.
如果这有帮助,这是我的工厂代码:
.factory('MinecraftServer',function($http){
function MinecraftServer(name, ip) { //does stuff }
MinecraftServer.prototype.acceptData = function(data){
data = data.data
if(data && data.online){
this.online = data.online;
//do more stuff
} else { // do more stuff } …Run Code Online (Sandbox Code Playgroud) 我正在使用angularJS,当我注入工厂时,我收到错误:
app.js:
angular.module('myapp', [])
Run Code Online (Sandbox Code Playgroud)
myfactory.js:
angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {
});
Run Code Online (Sandbox Code Playgroud)
控制器:test.js:
angular.module('myapp', [])
.controller('test', function($scope, $timeout, $sce, $http, httpService) {
$scope.submit = function() {
}
});
Run Code Online (Sandbox Code Playgroud)
当我添加httpService我得到错误.一切似乎都是对的,我甚至在所有项目中都使用这个工厂.错误:
angular.min.js:92 Error: [$injector:unpr] http://errors.angularjs.org/1.2.25/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService
at Error (native)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:6:450
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:202
at Object.c [as get] (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:270
at c (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at d (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:6)
at Object.instantiate (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:165)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:67:419
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:54:25
Run Code Online (Sandbox Code Playgroud)