使用 $scope acces Angular 重用函数
Reuse functions with $scope acces Angular
我正在解决在两个控制器中通过范围访问重用相同函数的问题
在这里描述:
How to include/inject functions which use $scope into a controller in angularjs?
在控制器中:
new CommonService($scope).getSomethingFromDB();
在工厂:
services.factory("CommonService", function (DBService) {
function Factory ($scope) {
this.$scope = $scope;
}
Factory.prototype.getSomethingFromDB = function () {
if( angular.isUndefined(this.$scope.vrsteObracuna) || this.$scope.vrsteObracuna === null) {
this.$scope.loading = true;
DBService.getSomethingFromDB(
function success(data) {
this.$scope.loading = false; //ERROR !!!
this.$scope.vrsteObracuna = data;
},
function error(data, status, headers, config) {
this.$scope.loading = false;
etna.notifications.error("Error fetching!");
}
)
}
return this.$scope.vrsteObracuna;
}
return Factory;
});
问题是 DBService.getSomethingFromDB 成功回调后
this.$scope.loading 未定义?
您还没有将 $scope 放入 "success" 闭包中,请尝试使用此代码:
services.factory("CommonService", function (DBService) {
function Factory ($scope) {
this.$scope = $scope;
}
Factory.prototype.getSomethingFromDB = function () {
if( angular.isUndefined(this.$scope.vrsteObracuna) || this.$scope.vrsteObracuna === null) {
this.$scope.loading = true;
var that = this;
DBService.getSomethingFromDB(
function success(data) {
that.$scope.loading = false; //ERROR !!!
that.$scope.vrsteObracuna = data;
},
function error(data, status, headers, config) {
that.$scope.loading = false;
etna.notifications.error("Error fetching!");
}
)
}
return this.$scope.vrsteObracuna;
}
return Factory;
});
我正在解决在两个控制器中通过范围访问重用相同函数的问题 在这里描述: How to include/inject functions which use $scope into a controller in angularjs?
在控制器中:
new CommonService($scope).getSomethingFromDB();
在工厂:
services.factory("CommonService", function (DBService) {
function Factory ($scope) {
this.$scope = $scope;
}
Factory.prototype.getSomethingFromDB = function () {
if( angular.isUndefined(this.$scope.vrsteObracuna) || this.$scope.vrsteObracuna === null) {
this.$scope.loading = true;
DBService.getSomethingFromDB(
function success(data) {
this.$scope.loading = false; //ERROR !!!
this.$scope.vrsteObracuna = data;
},
function error(data, status, headers, config) {
this.$scope.loading = false;
etna.notifications.error("Error fetching!");
}
)
}
return this.$scope.vrsteObracuna;
}
return Factory;
});
问题是 DBService.getSomethingFromDB 成功回调后 this.$scope.loading 未定义?
您还没有将 $scope 放入 "success" 闭包中,请尝试使用此代码:
services.factory("CommonService", function (DBService) {
function Factory ($scope) {
this.$scope = $scope;
}
Factory.prototype.getSomethingFromDB = function () {
if( angular.isUndefined(this.$scope.vrsteObracuna) || this.$scope.vrsteObracuna === null) {
this.$scope.loading = true;
var that = this;
DBService.getSomethingFromDB(
function success(data) {
that.$scope.loading = false; //ERROR !!!
that.$scope.vrsteObracuna = data;
},
function error(data, status, headers, config) {
that.$scope.loading = false;
etna.notifications.error("Error fetching!");
}
)
}
return this.$scope.vrsteObracuna;
}
return Factory;
});