我在我的项目中使用Angular UI $ modal http://angular-ui.github.io/bootstrap/#/modal
我不希望用户通过按背景来关闭模态.我想只能通过按下我创建的关闭按钮来关闭模态.
如何阻止模态关闭?
在下面的示例测试中,原始提供者名称是APIEndpointProvider,但是对于注入和服务实例化,约定似乎必须注入包含它的下划线.这是为什么?
'use strict';
describe('Provider: APIEndpointProvider', function () {
beforeEach(module('myApp.providers'));
var APIEndpointProvider;
beforeEach(inject(function(_APIEndpointProvider_) {
APIEndpointProvider = _APIEndpointProvider_;
}));
it('should do something', function () {
expect(!!APIEndpointProvider).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
我错过了一个更好的解释是什么?
是否可以在服务中设置一个$watch对象数组(我希望$watch声明本身在服务中)?
我正在使用Cordova和AngularJS开发移动应用程序.如何在Cordova设备准备好之前限制AngluarJS的引导.基本上我不希望在设备准备好之前使用任何AngularJS控制器.
我正在尝试通过遵循工厂方法文档中的示例来构建自己的服务.我认为我做错了但是因为我继续得到未知的提供程序错误.这是我的应用程序代码,包括声明,配置和工厂定义.
编辑我现在已经添加了所有文件来帮助排除故障
编辑错误的完整详细信息低于getSettings的问题,因为它正在寻找getSettingsProvider而无法找到它
Error: [$injector:unpr] http://errors.angularjs.org/1.2.16/$injector/unpr? p0=getSettingsProvider%20%3C-%20getSettings
at Error (native)
at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:6:450
at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:431
at Object.c [as get] (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13)
at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:499
at c (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13)
at d (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:230)
at Object.instantiate (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:394)
at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:66:112
at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:53:14 angular.js:9778
(anonymous function) angular.js:9778
(anonymous function) angular.js:7216
h.$apply angular.js:12512
(anonymous function) angular.js:1382
d angular.js:3869
$b.c angular.js:1380
$b angular.js:1394
Wc angular.js:1307
(anonymous function) angular.js:21459
a angular.js:2509
(anonymous function) angular.js:2780
q angular.js:330
c
Run Code Online (Sandbox Code Playgroud)
这些是我目前在我的应用程序中的所有文件
app.JS
//Initialize angular module include route dependencies
var app = angular.module("selfservice", ['ngRoute']);
app.config(function ($routeProvider) …Run Code Online (Sandbox Code Playgroud) 如何创建某种可以从我的所有控制器访问的utils包?
我的主模块中有这个路由代码:
'use strict';
angular.module('lpConnect', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/home', {template: 'views/home.html', controller: HomeCtrl}).
when('/admin', {template: 'views/admin.html', controller: AdminCtrl}).
when('/connect', {template: 'views/fb_connect.html', controller: MainAppCtrl}).
otherwise({redirectTo: '/connect'});
}]);
Run Code Online (Sandbox Code Playgroud)
我想要一个可以共同的功能HomeCtrl,AdminCtrl和MainAppCtrl.
我应该如何在AngularJS中做到这一点?
我有以下服务:
angular.module("services")
.factory("whatever", function($window) {
return {
redirect: function() {
$window.location.replace("http://www.whatever.com");
}
};
});
Run Code Online (Sandbox Code Playgroud)
如何$window在单元测试中模拟对象以防止在运行测试时重新加载页面?
我试过用
spyOn($window.location, 'replace').andReturn(true);
,但它没有工作(仍然有"Some of your tests did a full page reload!"错误)和
$provide.value('$window', {location: {replace: jasmine.createSpy()}})
,但我得到一个错误(Error: [ng:areq] Argument 'fn' is not a function, got Object),堆栈跟踪只指向角自己的源,所以它不是很有帮助...
我正在使用Angular UI bootstrap模式对话框并在服务中创建它:
myApp.factory('ModalService', ['$modal', function($modal) {
return {
trigger: function(template) {
$modal.open({
templateUrl: template,
size: 'lg',
controller: function($scope, $modalInstance) {
$scope.ok = function() {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
}
});
},
close: function() {
// this should close all modal instances
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
ModalService.close()从控制器调用时,如何关闭所有模态实例?
我正在使用AngularJS和TypeScript.我想使用Typescript类实现AngularJS服务,如下所示:
class HelloService {
public getWelcomeMessage():String {
return "Hello";
}
}
angular.module('app.services.helloService', []).factory('helloService', () => {
return new HelloService();
});
Run Code Online (Sandbox Code Playgroud)
这将编译为以下javascript代码:
var HelloService = (function () {
function HelloService() {
}
HelloService.prototype.getWelcomeMessage = function () {
return "Hello";
};
return HelloService;
})();
angular.module('app.services.helloService', []).factory('helloService', function () {
return new HelloService();
});
Run Code Online (Sandbox Code Playgroud)
这会使用变量HelloService污染全局命名空间,这显然是我不想要的.(使用Chrome的控制台我验证了HelloService是一个对象.)如何解决/避免此问题?
我试过了明显的事:
angular.module('app.services.helloService', []).factory('helloService', function () {
class HelloService { ...}
return new HelloService();
});
Run Code Online (Sandbox Code Playgroud)
但这给了我一个编译错误("意外的令牌;预期的'声明'.").
我能想到的一个可能的解决方案是以某种方式使用TypeScript的导入和导出,而这又将使用RequireJS.这可能会将HelloService包装在define函数中,从而避免使用HelloService污染全局范围.但是,我现在不想在我的AngularJS应用程序中使用RequireJS,因为我认为AngularJS足够好用于我的使用,并且它增加了复杂性.
所以,我的问题是,如何使用不污染全局范围的TypeScript类来定义AngularJS服务?
angularjs ×10
javascript ×3
angular-ui ×2
unit-testing ×2
cordova ×1
jasmine ×1
mocking ×1
typescript ×1
web ×1