我正在试图弄清楚如何模拟ElementRef注入组件的东西.我的组件如下:
app.component.ts:
import { Component, ElementRef } from '@angular/core';
import { AppService } from './app.service';
@Component({
selector: 'app-root',
templateUrl: './app/app.component.html',
styleUrls: ['./app/app.component.css']
})
export class AppComponent {
title = 'app works!';
constructor(private _elementRef: ElementRef, private _appService: AppService) {
console.log(this._elementRef);
console.log(this._appService);
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试规范如下:
app.component.spec.ts:
import { TestBed, async } from '@angular/core/testing';
import { ElementRef, Injectable } from '@angular/core';
import { AppComponent } from './app.component';
import { AppService } from './app.service';
@Injectable()
export class MockElementRef {
nativeElement: {}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有Angular JS的Boostrap 3工具提示,以便工具提示在Angular范围中显示对象的值.这在页面加载时工作正常,但是当更新范围中对象的值时,工具提示仍会显示原始值.
HTML:
<div ng-app>
<div ng-controller="TodoCtrl">
<span data-toggle="tooltip" data-placement="bottom" title="{{name}}">Hello {{name}}</span>
<button type="button" ng-click="changeName()">Change</button>
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
function TodoCtrl($scope) {
$scope.name = 'Ian';
$scope.changeName = function () {
$scope.name = 'Alan';
}
}
$(document).ready(function () {
$('span').tooltip();
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,有一个例子展示了我的代码以及这个小提琴中的问题