信使根据用户提供的输入显示搜索结果.在显示结果时,需要突出显示已搜索的单词.这些是使用的html和组件.
Component.html
<div *ngFor = "let result of resultArray">
<div>Id : result.id </div>
<div>Summary : result.summary </div>
<div> Link : result.link </div>
</div>
Run Code Online (Sandbox Code Playgroud)
Component.ts
resultArray : any = [{"id":"1","summary":"These are the results for the searched text","link":"http://www.example.com"}]
Run Code Online (Sandbox Code Playgroud)
通过发送搜索文本作为输入来获取此resultArray来命中后端服务.根据搜索文本,获取结果.需要突出显示搜索到的文字,类似于谷歌搜索.请找截图,
如果我搜索单词"member",则会突出显示单词"member"的出现.如何使用角度2实现相同.请提出一个想法.
我想对我的控制器进行单元测试,但我在读取值时遇到问题HttpContext.Session。
我想从我的控制器中模拟这一部分:HttpContext.Session.Get<int>(Foo)
使用Mock HttpContext 对 .NET core MVC 控制器进行单元测试?以及 如何在 asp net core 中模拟会话对象我能够得出以下解决方案:
我的测试:
public void GetFoos_AllGood_ReturnList()
{
//Arrange
Mock<ISession> sessionMock = new Mock<ISession>();
var fooSessionValue = new byte[0];
Web.Controllers.FooController fooController = new Web.Controllers.FooController();
fooController.ControllerContext.HttpContext = new DefaultHttpContext();
fooController.ControllerContext.HttpContext.Request.Headers["Foo"] = 0;
Mock<IServiceCollection> mock = new Mock<IServiceCollection>();
mock.Object.AddSession(); // Tried this, but failed
//Setup
sessionMock.Setup(_ => _.Set("Foo", It.IsAny<byte[]>())).Callback<string, byte[]>((k, v) => fooSessionValue = v);
sessionMock.Setup(_ => _.TryGetValue("Foo", out fooSessionValue)).Returns(true);
mockWebDataManager.Setup(b => b.GetFoos(It.IsAny<FooArgs>())).Returns(new FooResult() { …Run Code Online (Sandbox Code Playgroud) 信息
我的解决方案中有多个项目,其中一个是DAL,另一个是ASP.NET MVC6项目.由于MVC6项目也是启动项目,我需要在那里添加我的连接字符串.
我看到了这个解决方案,但它不被接受,也没有用.
我的尝试
appsettings.json
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"FooBar": {
"ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:FooBar:ConnectionString"]));
}
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试使用FooBar连接字符串访问数据时,我收到以下消息:
"附加信息:在应用程序配置文件中找不到名为'FooBar'的连接字符串."
问题
如何让多个连接字符串工作?
首先,我的班级:
export class FooBar {
...
isFavorite: boolean = false;
constructor() {
this.isFavorite = false;
}
}
Run Code Online (Sandbox Code Playgroud)
使用 Lodash 我对 的列表进行排序FooBar,因此我最喜欢的将在列表的顶部:
this.fooBars = _.orderBy(this.fooBars, ['isFavorite', 'name'], ['desc', 'asc']);
Run Code Online (Sandbox Code Playgroud)
当我收藏一个项目并查看我的 console.log 时,它说明了这一点:
请注意,#3 没有 isFavorite 属性...
每当我从未设置时,isFavorite它都不会显示。这使得 Lodash 排序错误。
有没有办法始终显示此属性,即使它未使用/未设置/错误?
我试过:
- 在类
中将属性设置为 false
-在类的构造函数中将属性设置为 false -this.foobars在我的组件中循环,将它们全部设置为 false
- 添加一个接口到FooBar
我有一个工作异步验证器,它向服务器发出 HTTP 请求以检查用户名是否已被占用。因为我不想在每次击键后调用 API,所以我需要对输入流进行去抖动。
我第一次参加throttleTime了这项服务,但是关于 SO 的另一个主题说这必须是订阅者,但还没有运气!
我的组件:
this.form = this._fb.group(
{
username: ['', [Validators.required, Validators.maxLength(50), NoWhitespaceValidator], [IsUserIdFreeValidator.createValidator(this._managementService)]]
});
Run Code Online (Sandbox Code Playgroud)
我的验证器:
export class IsUserIdFreeValidator {
static createValidator(_managementService: ManagementService) {
return (control: AbstractControl) => {
return _managementService.isUserIdFree(control.value)
.pipe(
throttleTime(5000),
(map(
(result: boolean) => result === false ? { isUserIdFree: true } : null))
);
};
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务:
public isUserIdFree(userId: string): Observable<{} | boolean | HttpError> {
const updateUserCheck: UpdateUserCheck = new UpdateUserCheck();
updateUserCheck.userID = userId;
return this._httpClient.post<boolean>('UserManagementUser/IsUserIdFree', updateUserCheck));
}
Run Code Online (Sandbox Code Playgroud) 我想通过 Angular 在我的 ASP.NET Core MVC API 控制器中填充一个字符串参数。
我有这个工作电话:
应用程序接口
//A class to wrap my string
public class Foo
{
public string bar { get; set; }
}
[HttpPost]
public JsonResult GetDetails([FromBody] Foo bar) { ... }
Run Code Online (Sandbox Code Playgroud)
角度(服务)
public get() {
let bar: any = new Object();
bar.foo = 'Hello World';
return this._httpClient.post('api/GetDetails', bar).toPromise();
}
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是传递一个字符串而不必将它包装在这样的类中:
应用程序接口
[HttpPost]
public JsonResult GetDetails([FromBody] string bar) { ... }
Run Code Online (Sandbox Code Playgroud)
角度(服务)
public get() {
let bar: string = "Hello World";
return this._httpClient.post('api/GetDetails', bar).toPromise();
} …Run Code Online (Sandbox Code Playgroud) 我编写了一个 ActionFilter 来检查我的模型状态是否有效(这样我就可以记录问题):
public class ValidationFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{ ... }
}
Run Code Online (Sandbox Code Playgroud)
我的断点从未被命中,我在 Postman 中得到了输出:
{ "errors": {
"RequiredText": [
"Test - requiredText is required"
]
},
"title": "发生一个或多个验证错误。",
"status": 400,
"traceId": "0HLOBA7E4R7SL:00000002"
}
我假设 .NET Core 预先进行了验证。
有没有办法关闭此功能,以便使用我的属性?
angular ×4
typescript ×4
asp.net-core ×3
c# ×3
javascript ×3
json ×3
.net-core ×1
asp.net-mvc ×1
html5 ×1
lodash ×1
mocking ×1
moq ×1
rxjs ×1
xunit ×1