将$ scope注入角度服务函数()
我有一个服务:
angular.module('cfd')
.service('StudentService', [ '$http',
function ($http) {
// get some data via the $http
var path = 'data/people/students.json';
var students = $http.get(path).then(function (resp) {
return resp.data;
});
//save method create a new student if not already exists
//else update the existing object
this.save = function (student) {
if (student.id == null) {
//if this is new student, add it in students array
$scope.students.push(student);
} else {
//for existing student, find this student using id
//and update it.
for (i in students) {
if (students[i].id == student.id) {
students[i] = student;
}
}
}
};但是当我打电话时save(),我无法访问$scope,并得到ReferenceError: $scope is not defined。所以逻辑步骤(对我而言)是提供save()$scope,因此我还必须提供/注入它service。所以,如果我这样做:
.service('StudentService', [ '$http', '$scope',
function ($http, $scope) {我收到以下错误:
错误:[$ injector:unpr]未知提供者:$ scopeProvider < - $ scope < - StudentService
错误中的链接(哇哇哇!)让我知道它与注入器相关,并且可能与js文件的声明顺序有关。我曾尝试重新排序它们index.html,但我认为它更简单,例如我注入它们的方式。
使用Angular-UI和Angular-UI-Router
largeQ
慕运维8079593
侃侃尔雅
随时随地看视频慕课网APP
相关分类