我很难尝试在Angularjs中测试基于promise的代码.
我的控制器中有以下代码:
$scope.markAsDone = function(taskId) {
tasksService.removeAndGetNext(taskId).then(function(nextTask) {
goTo(nextTask);
})
};
function goTo(nextTask) {
$location.path(...);
}
Run Code Online (Sandbox Code Playgroud)
我想对以下情况进行单元测试:
markAsDone
叫它应该打电话tasksService.removeAndGetNext
tasksService.removeAndGetNext
完成它应该改变位置(调用goTo
)在我看来,没有简单的方法来分别测试这两个案例.
我测试第一个测试的是:
var noopPromise= {then: function() {}}
spyOn(tasksService, 'removeAndGetNext').andReturn(noopPromise);
Run Code Online (Sandbox Code Playgroud)
现在要测试第二种情况,我需要创建另一个永远的假承诺resolved
.这一切都非常乏味,而且它是很多样板代码.
有没有其他方法来测试这样的东西?或者我的设计闻到了什么?
我有两个连接的ui可排序列表.当其中一个列表为空时,我需要显示一条消息; 当拖动时悬停该空列表时,我需要显示一个样式化的放置目标并隐藏空列表消息.我能够编写绝大多数代码,这里有一个简化的Codepen工作.
问题在于,当您从填充列表中拖动空列表然后再次移出时,空列表会显示空列表占位符和样式化放置目标.这是一个截屏:
问题的根源似乎在于我计算sortableList指令的列表是否为空:
scope.isEmpty = function() {
if (!scope.attachments) {
return true;
} else if (scope.dragDirection === 'drag-out' && !scope.hovered) {
return scope.attachments.length <= 1;
} else if (scope.hovered) {
return false;
} else {
return scope.attachments.length === 0;
}
};
Run Code Online (Sandbox Code Playgroud)
请注意,我正在跟踪范围上的状态并使用$ apply来确保DOM更新如下:
function onDragStart() {
scope.$apply(function() {
scope.dragDirection = 'drag-out';
});
}
function onDragStop() {
scope.$apply(function() {
scope.dragDirection = '';
});
}
function onDragOver() {
scope.$apply(function() {
scope.hovered = true;
});
}
function onDragOut() {
scope.$apply(function() { …
Run Code Online (Sandbox Code Playgroud) 我有两个服务:LoginService和UserService.我试图将UserService注入LoginService,该应用程序将无法运行.
在控制台中,我有错误:
错误:无法解析UserService的所有参数:([object Object],?).在SyntaxError.ZoneAwareError(http:// localhost:4200/polyfills.bundle.js:7156:33)处于SyntaxError.BaseError [作为构造函数]
这是我的LoginService:
import {UserService} from '../services/user.service';
@Injectable()
export class LoginService {
constructor(public _http: Http,public us:UserService) {
}
Run Code Online (Sandbox Code Playgroud)
和UserService:
@Injectable()
export class UserService {
constructor(private _http: Http , public _ls: LoginService) {
}
Run Code Online (Sandbox Code Playgroud)
角度模块:
import {LoginService} from './services/login.service';
import {UserService} from './services/user.service';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
],
exports: [BrowserModule, SlimLoadingBarModule],
providers: [UserService, LoginService, appRoutingProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中实现了延迟加载。我的一项服务需要包含 DecimalPipe。
服务 --> 共享模块 --> 应用模块
这是我的结构。我已经在 app.module.ts 中包含了“CommonModule”,我的服务也需要 Decimal Pipe。
类型 DecimalPipe 是 2 个模块声明的一部分:CommonModule 和 SharedModule!请考虑将 DecimalPipe 移至导入 CommonModule 和 SharedModule 的更高模块。您还可以创建一个新的 NgModule 来导出并包含 DecimalPipe,然后在 CommonModule 和 SharedModule 中导入该 NgModule。
所以既然它已经是 Commons Module 的一部分,为什么不从 Commons Module 中取出 Decimal 管道呢?? 如果未声明,则会显示以下错误
NullInjectorError:DecimalPipe 没有提供程序!
请让我知道如何处理此错误。提前致谢!
当我运行以下命令时
ng set --global packageManager=yarn
Run Code Online (Sandbox Code Playgroud)
我收到以下消息
"get/set have been deprecated in favor of the config command"
Run Code Online (Sandbox Code Playgroud)
当我跑步时——
ng config cli.packageManager yarn
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息
Cannot read property 'value' of null
TypeError: Cannot read property 'value' of null
at ConfigCommand.set (C:\Users\Rahul\AppData\Roaming\npm\node_modules\@angular\cli\commands\config.js:207:36)
at ConfigCommand.run (C:\Users\Rahul\AppData\Roaming\npm\node_modules\@angular\cli\commands\config.js:181:18)
at C:\Users\Rahul\AppData\Roaming\npm\node_modules\@angular\cli\models\command-runner.js:278:30
at Generator.next (<anonymous>)
at fulfilled (C:\Users\Rahul\AppData\Roaming\npm\node_modules\@angular\cli\models\command-runner.js:4:58)
at <anonymous>
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
以下是我使用 Java 8 的版本信息。
NPM version - 5.6.0
Yarn version --1.7.0
Run Code Online (Sandbox Code Playgroud)
ng - v 给出了以下结果。我想使用 Angular js
Angular CLI: 6.0.8
Node: 8.11.3
OS: win32 x64
Angular:
...
Package …
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,它使用 ngx-bootstrap 在鼠标悬停时显示工具提示。我想测试动态添加的内容是否正确显示。为了做到这一点,我有一个看起来像这样的测试:
it(shows the right tooltip', fakeAsync(() => {
fixture.debugElement.query(By.directive(TooltipDirective))
.triggerEventHandler('mouseover', null);
tick();
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.tooltip-inner')).nativeElement)
.toBe('the tooltip text');
}
Run Code Online (Sandbox Code Playgroud)
这会导致一个错误,表明fixture.debugElement.query(By.css('.tooltip-inner'))
:“无法读取 null 的属性 'nativeElement'”
如果我打印出fixture.debugElement.nativeElement
我得到的内容:
<div id="root1" ng-version="5.2.9">
<my-component>
<div ng-reflect-tooltip="the tooltip text">
<img src="images/test.png">
</div>
<bs-tooltip-container role="tooltip" class="tooltip in tooltip-right">
<div class="tooltip-arrow arrow"></div>
<div class="tooltip-inner">the tooltip text</div>
</bs-tooltip-container>
<my-component>
</div>
Run Code Online (Sandbox Code Playgroud)
重要的一点是 html 存在 - 它只是无法通过DebugElement.query
.
我目前获得规范通过的解决方案是将期望更改为:
expect(fixture.debugElement.nativeElement.textContent.trim())
.toBe('the tooltip text');
Run Code Online (Sandbox Code Playgroud)
这是有效的,但如果我遇到具有多个工具提示的类似情况(例如),它会崩溃。有没有人能够以更好的方式处理这个问题?我没有正确设置这个规范吗?
我试图按照新文件文档中的说明使用curl来关闭正常运行时间监控:https://docs.newrelic.com/docs/alerts/availability-monitor-settings
根据此文档,此请求将禁用我的正常运行时间监视:
curl https://www.myhost.com/accounts/acct_id/applications/app_id/ping_targets/disable -X POST -H"X-Api-Key:"my_key"
这是我正在使用的请求(删除了个人信息):卷曲" https://api.newrelic.com/api/v1/accounts/NNNNNN/applications/NNNNNNN/ping_targets/disable"-X POST -H"X -API密钥:X ... X"
我已经成功地将我的acct_id,app_id和x-api-key用于其他GET命令.但是,当我尝试上述命令时,我收到此错误:
<hash>
<error>The requested page could not be found</error>
</hash>
Run Code Online (Sandbox Code Playgroud)
我是curl命令的新手所以这可能只是我的结构的一个问题,但似乎url是不正确的.根据这个堆栈交换帖子:通过rest api打开或关闭Newrelic监控没有办法做到这一点,但这与文档冲突.
那么,有没有办法实现这一目标?有没有人成功完成这个?
我试图添加动态设置a的高度div
,但它没有成功设置.我得到窗口的高度并将其除以3,但它不起作用.这是我设置高度的地方:
<div id="wrapper" ng-style="height :hgt+'px'">
Run Code Online (Sandbox Code Playgroud)
我正在设置范围变量,如下所示:
$scope.hgt = $window.innerHeight / 3;
Run Code Online (Sandbox Code Playgroud)
但是没有设定高度.
angularjs angularjs-directive angularjs-scope angularjs-ng-repeat
我已使用以下步骤将引导程序包含到我的 Angular 项目中
该样式未应用于我的页面。还有什么我必须做的吗?我正在从索引页添加 HTML 代码片段
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Project</title>
<script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap-theme.min.css">
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>ewreeew</h1>
<button class="btn btn-primary">Test Button</button>
<app-root></app-root>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
节点:8.11.2
操作系统:win32 x64
角度:6.0.5
...动画、常见、编译器、编译器-cli、核心、表单
... http、语言服务、平台浏览器
...平台浏览器动态、路由器
@Angular-devkit/架构师 0.6.8
@Angular-devkit/build-Angular 0.6.8
@Angular-devkit/构建优化器 0.6.8
@Angular-devkit/核心 0.6.8
@angular-devkit/schematics 0.6.8
@角度/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/角度0.6.8
@schematics/更新0.6.8
rxjs 6.2.1
打字稿 2.7.2
网页包 4.8.3 …
angular-ui-bootstrap angular-cli angular angular6 angular-cli-v6
我有一个服务,每隔 10 秒检查一次数据状态以更新组件中的标签。仅当位于组件所在的页面上时才需要这样做。该组件看起来有点像这样:
@Component({
selector: 'editor-status',
templateUrl: './editorStatus.component.html',
providers: [EditorStatusService]
})
export class EditorStatusComponent implements OnDestroy {
constructor(private service: EditorStatusService){};
public ngOnDestroy(): void {
service.destroy();
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务有这样的结构:
@Injector()
export class EditorStatusService {
private intervalId: any;
constructor() {
this.intervalId = setInterval(() => {
/* code to update ui */
}, 10000);
}
public destroy(): void {
clearInterval(this.intervalId);
}
}
Run Code Online (Sandbox Code Playgroud)
是否每次实例化组件时都会以这种方式构建提供给组件的服务?如果我不销毁侦听器,每次加载此页面然后导航离开时是否会造成内存泄漏?
我正在从 Momentjs 迁移到 Luxon。我允许用户选择任务需要的小时数。当我们保存这个值时,我希望它能够正常化。因此,如果一项任务需要 90 小时,我更愿意存储 3 天零 18 小时。我存储 ISO 持续时间字符串,因此我想将 PT90H 转换为 P3DT18H...当然可以使用任何给定值。
我尝试过 Luxon 的normalize
函数,我尝试这样调用它:
const duration = Duration.fromObject({ hours: 90 });
duration.normalize().toISO();
Run Code Online (Sandbox Code Playgroud)
然而,normalize
似乎对结果没有做任何事情。我仍然得到 PT90H。Luxon 是否有内置方法可以做到这一点?
如果用户未使用 AngularFireAuth 进行身份验证,我正在编写一个警卫来将用户重定向到登录页面。如果没有这个库,我会在本地/会话存储中存储一个令牌,当守卫触发时,我会检查存储以查看是否存在令牌。AngularFireAuth 在数据库中存储一个令牌(其中包含身份验证信息):
然而,直接检查它是不明智的,因为 AngularFireAuth 可能会在未来的任何日期改变它的工作方式。它不完全是记录在案的公共 API 的一部分。
在旧版本的 AngularFireAuth 中,我可以在我的服务中包含这个方法来做这个检查:
import { AngularFireAuth } from '@angular/fire/auth';
export class AuthService {
constructor(private readonly service: AngularFireAuth) {}
public login(credentials: Credentials) {
const { email, password } = credentials;
return this.service.auth.signInWithEmailAndPassword(email, password);
}
public isLoggedIn(): boolean {
return !!this.service.auth.currentUser;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会在刷新时持续存在。因此,当我在系统中导航时,它工作正常,但如果我刷新或离开并返回,系统会提示我再次登录。
我如何编写我的isLoggedIn
方法可以在我的守卫中调用?
angular ×6
angularjs ×3
angular-cli ×2
javascript ×2
angular-ui ×1
angular6 ×1
angularfire2 ×1
curl ×1
date ×1
firebase ×1
heroku ×1
inject ×1
jasmine ×1
luxon ×1
newrelic ×1
promise ×1
service ×1
testing ×1
typescript ×1
yarnpkg ×1