我有一个角度服务,在其构造函数中侦听 $rootscope 上的事件。
该服务未注入应用程序中的任何位置,因此未初始化。为了解决这个问题,我们只是注入它,而不是在另一个服务中使用它,只是为了让它“更新”。
有没有某种方法可以初始化服务而不必将其注入其他服务/控制器/指令中?
TL;DR:与 angular 无关的静态基本功能 - 实现为 AngularJS 服务还是普通的静态导出类/方法?
长版:我已经在 TS 中编程大约一个月了,因为我们正在重构我们的应用程序以使用 TS(为 angular 2.0 做准备)。当开始了解我们的一些基本 Angular 服务时,我在想——因为实现静态类和方法很容易——也许我们的一些服务根本不应该是服务。当然,以某种方式与角度有关的任何功能都必须作为服务来实现。例如,ColorConverter 或 ColorPicker——今天我们应用程序中的 angularjs 服务实现了不会改变或与 angular 或任何共享外部资源有关的静态逻辑——可以很容易地替换为导出静态函数的静态模块。我办公室里有人提出支持 Angular 服务的一个论点是,稍后我们可以轻松地嘲笑这个逻辑。但是为什么我要模拟不会更改且不访问任何外部资源的静态逻辑?一分钱你的想法。
我目前正在试验AngularJS.我创建了一个简单的服务,它是从一些远程数据初始化的.url作为应用程序值传递.
//App declaration
angular.module('myApp', ['myApp.services']).value('someUrl', 'http://www.example.com');
//Service declaration
angular.module('myApp.services', ['someUrl']).factory('MyService', function(someUrl) {
var data;
"Do stuff"
return data;
});
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试对此进行单元测试,但我无法正确设置"someUrl"参数.我尝试过这样的事情,没有成功:
angular.module('myApp', ['myApp.services']).value('someUrl', 'test/test.json');
describe('Services', function() {
beforeEach(module('myApp.services'));
describe('MyService', function() {
it('should return {success: true}', inject(function() {
expect(data).toEqual({success: true});
}));
});
});
Run Code Online (Sandbox Code Playgroud)
但是,我总是收到"No module:someUrl"错误.在单元测试期间初始化app值的正确语法是什么?
在尝试回答有关在两个独立控制器之间共享数据的问题时,我遇到了一个问题.
我通常使用服务来完成这项任务并开始创建一个jsfiddle,但我无法让它工作.
经过一些调试后,如果我在setActivePersonWorks(person)脏检查中动态创建属性,第二个控制器显示正确的值.
如果我分配了setActivePersonDoesNotWork()它的值没有.
如果我使用$timeout()我能够验证DataService.badPerson确实包含正确的数据.
难道我做错了什么?我想如果你做了什么,$apply()它会正常工作,但为什么动态创建值会导致事情正常工作?
工作实例:
var myTest = angular.module("MyTest", []);
myTest.factory("DataService", function () {
var People = {
goodPerson: {},
badPerson: {},
setActivePersonWorks: function (person) {
People.goodPerson.name = person.name;
People.goodPerson.id = person.id;
},
setActivePersonDoesNotWork: function (person) {
People.badPerson = person;
}
};
return People;
});
function ViewController($scope, DataService, $timeout) {
$timeout(function () {
DataService.setActivePersonWorks({
id: 1,
name: "Good Mark"
});
DataService.setActivePersonDoesNotWork({
id: 2,
name: "Bad Mark"
});
}, …Run Code Online (Sandbox Code Playgroud) 如何创建工厂$ resource或$ http服务来处理来自外部资源的具有多个任意参数的查询字符串?例如.
#/animals
#/animals?gender=m
#/animals?color=black
#/animals?size=small
#/animals?gender=m&color=black
#/animals?size=med&gender=f
#/animals?size=lg&gender=m&color=red
Run Code Online (Sandbox Code Playgroud)
该想法是存在按钮/输入,用户可以按下该按钮/输入以添加到查询字符串中的当前参数列表以获得具有不同组合的期望属性的新动物列表.我已经尝试了以下但它没有根据需要重新加载或添加新参数到现有的URL.另外,我不确定是否应该在控制器或工厂中调用$ route.current.params,如果这是最好的方法.
angular.module(Animals, ['$resource', '$route', '$location', function($resource, $route, $location) {
return $resource('http://thezoo.com/animals', $route.current.params, { query: {method: 'GET', isArray: true}});
}]);
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我有一些工厂:
.factory("someFactory", function() {
var someFactory = {};
someFactory.objects = [
{ name: "obj1" },
{ name: "obj2" }
];
return someFactory;
}
Run Code Online (Sandbox Code Playgroud)
还有一些控制器,它监视这个工厂对象名称的变化:
.controller("someController", ["someFactory", "$scope", function($scope, someFactory) {
for (var i = someFactory.values.length - 1; i >= 0; i--) {
$scope.$watch("someFactory.values[" + i + "].name", function(newVal, oldVal, scope) {
if(newVal !== undefined) {
// do something
}
});
}]);
Run Code Online (Sandbox Code Playgroud)
我很惊讶这实际上是有效的,我不必单独为我工厂中的每个对象编写代码.我想我很惊讶,因为我对$ scope的实际缺乏了解.如果有人问我,我会告诉他这就像是观察者的好模式.这是一个合适的解释吗?
angularjs angularjs-service angularjs-scope angularjs-controller
这段代码给了我Error: [$injector:unpr] Unknown provider: $scope, $locationProvider <- $scope, $location.
var app = angular.module('myApp.controllers', []);
app.controller('Signup', ['$scope, $location', function($scope, $location) {
$scope.checkEmailValid = function(){
//TODO make a decision about whether to go somewhere, if true do this:
$location.path('/view2');
};
}]);
Run Code Online (Sandbox Code Playgroud)
我错过了有关如何注入位置服务的内容吗?我没有配置$ locationProvider,但这样做似乎没有帮助.
我有一个服务:
angular.module('USC').service('TemplateService', function() {});
Run Code Online (Sandbox Code Playgroud)
我想在手动引导我的Angular项目之前使用它:
angular.bootstrap(document, ['USC']);
Run Code Online (Sandbox Code Playgroud)
这可能吗?我知道我可以调用var service = angular.bootstrap().get(); 对于Angular提供的服务,但是如何在模块初始化之前调用自定义服务?
我试图将字符串转换为控制器中的AngularJS服务方法调用.例如,我想转换字符串"Contact.send(email)"来调用现有的服务方法.我想用:
window["Contact"]["send"](email);
就像在这个线程中一样 - 当我把它的名字作为一个字符串时如何执行JavaScript函数 - 但它表示Contact服务是未定义的,尽管被注入到控制器中.
我的应用程序中有多个控制器,其中有一些重复的代码,例如:
$scope.alert = null;
$scope.addAlert = function (message) {
$scope.alert = { type: 'danger', msg: message };
};
$scope.clearAlerts = function () {
$scope.alert = null;
};
Run Code Online (Sandbox Code Playgroud)
在AngularJS中共享这些范围函数和变量的推荐方式是什么?使用控制器继承?
javascript angularjs angularjs-service angularjs-scope angularjs-controller