更多的问题;“易学”;angularjs框架
我正在尝试做一件最简单的事情,这显然让AngularJS感到难以置信的困难 此代码在这里起作用:更多的问题;“易学”;angularjs框架,angularjs,Angularjs,我正在尝试做一件最简单的事情,这显然让AngularJS感到难以置信的困难 此代码在这里起作用: .factory('ItemService', [function() { var items = [ {id: 1, label: 'Item 0'}, {id: 2, label: 'Item 1'} ]; return { list: function() { return items; }, add: function(item) { items.push
.factory('ItemService', [function() {
var items = [
{id: 1, label: 'Item 0'},
{id: 2, label: 'Item 1'}
];
return {
list: function() {
return items;
},
add: function(item) {
items.push(item);
}
};
变量项在工厂顶部声明,可以在return语句中访问
那么,为什么这不起作用:
.factory('ItemService', ['$http', function($http) {
var self = this;
self.items = [];
$http.get('/api_job_inspections/1/edit').then(function(response) {
//self.items = response.data;
self.items = [
{id: 1, label: 'Item 0'},
{id: 2, label: 'Item 1'}
];
}, function(errResponse) {
console.error('Error while fetching notes');
});
return {
list: function() {
return self.items;
},
add: function(item) {
self.items.push(item);
}
};
我曾尝试使用var items=[]如第一个示例中所示,但没有返回任何结果。使用此语法不会返回任何内容。为什么?似乎存在范围界定问题,但它是什么
提前感谢您…这似乎不是范围问题,而是异步问题,您是否确保ajax调用已完成 尝试在控制器中执行以下操作:
angular.module('myApp').controller('ItemsController',
['ItemService', '$scope', function(items, $scope) {
$scope.$watch(function() {
return items.list();
}, function(newVal) {
console.log(newVal);
});
}]);
p、 它是一个围绕工厂的包装器,基本上允许您使用
this.something=function
它比工厂(imo)方便得多。这似乎不是一个范围问题,而是一个异步问题,您确保ajax调用完成了吗
尝试在控制器中执行以下操作:
angular.module('myApp').controller('ItemsController',
['ItemService', '$scope', function(items, $scope) {
$scope.$watch(function() {
return items.list();
}, function(newVal) {
console.log(newVal);
});
}]);
p、 它是一个围绕工厂的包装器,基本上允许您使用
this.something=function
它比工厂(imo)方便得多。这似乎不是一个范围问题,而是一个异步问题,您确保ajax调用完成了吗
尝试在控制器中执行以下操作:
angular.module('myApp').controller('ItemsController',
['ItemService', '$scope', function(items, $scope) {
$scope.$watch(function() {
return items.list();
}, function(newVal) {
console.log(newVal);
});
}]);
p、 它是一个围绕工厂的包装器,基本上允许您使用
this.something=function
它比工厂(imo)方便得多。这似乎不是一个范围问题,而是一个异步问题,您确保ajax调用完成了吗
尝试在控制器中执行以下操作:
angular.module('myApp').controller('ItemsController',
['ItemService', '$scope', function(items, $scope) {
$scope.$watch(function() {
return items.list();
}, function(newVal) {
console.log(newVal);
});
}]);
p、 在美国,你可以使用服务,而不是使用工厂,它是工厂的包装器,基本上允许你使用
this.something=function
它比工厂(imo)方便得多。在你的第一个例子中,项在函数返回之前可用,而在第二个示例中,您正在进行异步调用self.items
在您的服务注入控制器时还没有填充
这两个示例根本不同,因此需要向服务使用者(即控制器)公开不同的API
您应该考虑将<代码>加载项< /代码>函数添加到您的服务中:
...
loadItems: function(){
return $http.get('/api_job_inspections/1/edit')
.then(function(response){
self.items = response.data;
return self.items;
},
function(errResponse) {
console.error('Error while fetching notes');
});
},
...
在控制器中,您可以执行以下操作:
.controller("myCtrl", function($scope, ItemService){
ItemService.loadItems()
.then(function(items)){
$scope.items = items;
// ItemService.list will also be available, so you can do:
// $scope.items = ItemService.list;
}
})
这与所建议的使用.factory
与.service
无关
另外,不需要使用$q创建延迟对象。延迟-$http
已返回。然后-able承诺。在第一个示例中,项在函数返回之前可用,而在第二个示例中,您正在进行异步调用self.items
在您的服务注入控制器时还没有填充
这两个示例根本不同,因此需要向服务使用者(即控制器)公开不同的API
您应该考虑将<代码>加载项< /代码>函数添加到您的服务中:
...
loadItems: function(){
return $http.get('/api_job_inspections/1/edit')
.then(function(response){
self.items = response.data;
return self.items;
},
function(errResponse) {
console.error('Error while fetching notes');
});
},
...
在控制器中,您可以执行以下操作:
.controller("myCtrl", function($scope, ItemService){
ItemService.loadItems()
.then(function(items)){
$scope.items = items;
// ItemService.list will also be available, so you can do:
// $scope.items = ItemService.list;
}
})
这与所建议的使用.factory
与.service
无关
另外,不需要使用$q创建延迟对象。延迟-$http
已返回。然后-able承诺。在第一个示例中,项在函数返回之前可用,而在第二个示例中,您正在进行异步调用self.items
在您的服务注入控制器时还没有填充
这两个示例根本不同,因此需要向服务使用者(即控制器)公开不同的API
您应该考虑将<代码>加载项< /代码>函数添加到您的服务中:
...
loadItems: function(){
return $http.get('/api_job_inspections/1/edit')
.then(function(response){
self.items = response.data;
return self.items;
},
function(errResponse) {
console.error('Error while fetching notes');
});
},
...
在控制器中,您可以执行以下操作:
.controller("myCtrl", function($scope, ItemService){
ItemService.loadItems()
.then(function(items)){
$scope.items = items;
// ItemService.list will also be available, so you can do:
// $scope.items = ItemService.list;
}
})
这与所建议的使用.factory
与.service
无关
另外,不需要使用$q创建延迟对象。延迟-$http
已返回。然后-able承诺。在第一个示例中,项在函数返回之前可用,而在第二个示例中,您正在进行异步调用self.items
在您的服务注入控制器时还没有填充
这两个示例根本不同,因此需要向服务使用者(即控制器)公开不同的API
您应该考虑将<代码>加载项< /代码>函数添加到您的服务中:
...
loadItems: function(){
return $http.get('/api_job_inspections/1/edit')
.then(function(response){
self.items = response.data;
return self.items;
},
function(errResponse) {
console.error('Error while fetching notes');
});
},
...
在控制器中,您可以执行以下操作:
.controller("myCtrl", function($scope, ItemService){
ItemService.loadItems()
.then(function(items)){
$scope.items = items;
// ItemService.list will also be available, so you can do:
// $scope.items = ItemService.list;
}
})
这与所建议的使用.factory
与.service
无关
另外,不需要使用$q创建延迟对象。延迟-$http
已返回。然后-able承诺。这是一个异步问题。您可以使用$q
:
app.factory('ItemService', ['$http', '$q',
function($http, $q) {
var self = this;
var service = {};
self.items = [];
service.list = function(callback) {
var deferred = $q.defer();
var cb=callback||angular.noop;
$http.get('/api_job_inspections/1/edit').success(function(data) {
//self.items = data;
self.items = [{
id: 1,
label: 'Item 0'
}, {
id: 2,
label: 'Item 1'
}];
deferred.resolve(self.items);
return cb();
}).error(function(err) {
console.error('Error while fetching notes');
deferred.reject(err);
return cb(err);
}.bind(this));
return deferred.promise;
};
service.add=function(item){
self.items.push(item);
};
return service;
}
]);
然后在控制器中,您可以像这样使用此服务:
ItemService.list().then(function(data){
$scope.items=data;
console.log(data);
});
这是一个异步问题。您可以使用$q
:
app.factory('ItemService', ['$http', '$q',
function($http, $q) {
var self = this;
var service = {};
self.items = [];
service.list = function(callback) {
var deferred = $q.defer();
var cb=callback||angular.noop;
$http.get('/api_job_inspections/1/edit').success(function(data) {
//self.items = data;
self.items = [{
id: 1,
label: 'Item 0'
}, {
id: 2,
label: 'Item 1'
}];
deferred.resolve(self.items);
return cb();
}).error(function(err) {
console.error('Error while fetching notes');
deferred.reject(err);
return cb(err);
}.bind(this));
return deferred.promise;
};
service.add=function(item){
self.items.push(item);
};
return service;
}
]);
然后在控制器中,您可以像这样使用此服务:
ItemService.list().then(function(data){
$scope.items=data;
console.log(data);
});
这是一个异步问题。您可以使用$q
:
app.factory('ItemService', ['$http', '$q',
function($http, $q) {
var self = this;
var service = {};
self.items = [];
service.list = function(callback) {
var deferred = $q.defer();
var cb=callback||angular.noop;
$http.get('/api_job_inspections/1/edit').success(function(data) {
//self.items = data;
self.items = [{
id: 1,
label: 'Item 0'
}, {
id: 2,
label: 'Item 1'
}];
deferred.resolve(self.items);
return cb();
}).error(function(err) {
console.error('Error while fetching notes');
deferred.reject(err);
return cb(err);
}.bind(this));
return deferred.promise;
};
service.add=function(item){
self.items.push(item);
};
return service;
}
]);
然后在控制器中,您可以像这样使用此服务:
ItemService.list().then(function(data){
$scope.items=data;
console.log(data);
});
这是一个异步问题。您可以使用$q
:
app.factory('ItemService', ['$http', '$q',
function($http, $q) {
var self = this;
var service = {};
self.items = [];
service.list = function(callback) {
var deferred = $q.defer();
var cb=callback||angular.noop;
$http.get('/api_job_inspections/1/edit').success(function(data) {
//self.items = data;
self.items = [{
id: 1,
label: 'Item 0'
}, {
id: 2,
label: 'Item 1'
}];
deferred.resolve(self.items);
return cb();
}).error(function(err) {
console.error('Error while fetching notes');
deferred.reject(err);
return cb(err);
}.bind(this));
return deferred.promise;
};
service.add=function(item){
self.items.push(item);
};
return service;
}
]);
然后在控制器中,您可以像这样使用此服务:
ItemService.list().then(function(data){
$scope.items=data;
console.log(data);
});
您希望什么时候可以退货?如果您想退货,请使用服务而不是工厂?如果您想退货,请使用服务而不是工厂