在这里使用 Angular 6:
我有一个父组件,其中有 3 个子组件。子组件 1 有一个文本字段,子组件 2 有一个下拉列表,子组件 3 有一个下拉列表和一个提交按钮。
在子 3 的提交单击按钮上,我想验证 child1、child2 和 child3 的所有输入(因为它们是必需的)并相应地抛出错误。
使用 AngularJS,我可以绑定到 ngmodel 并检查表单是否无效。我怎样才能在角度上做到这一点?我的意思是如何获取输入状态并将其从一个孩子传递给另一个孩子等。在搜索时,我发现了反应形式的概念,但大多数文章都是仅具有输入的父母形式。
如果有人可以提供帮助,我将不胜感激。下面是我的代码
- 更新 -
我在关注这篇文章后更新了以下代码: https: //medium.com/@joshblf/using-child-components-in-angular-forms-d44e60036664
但是这个在控制台中不断给我错误:“错误:formControlName必须与父formGroup指令一起使用。您需要添加一个formGroup指令并向其传递一个现有的FormGroup实例(您可以在类中创建一个实例)。 ”
--以反应形式更新了代码--
--Parent--
<form class="form-horizontal" [formGroup]="myForm">
<div class="row">
<ct-child1 [myForm]="myForm"></ct-child1>
<ct-child2> </ct-child2>
</div>
<div class="row">
<ct-child3></ct-child3>
</div>
</form>
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'ct-parent',
templateUrl: './parent.component.html']
})
export class ParentComponent implements OnInit {
myForm: FormGroup;
constructor(private …Run Code Online (Sandbox Code Playgroud) 使用.NET core MVC C#
我有一个控制器:
[ServiceFilter(typeof(MyCustomFilter))]
public class HomeController : Controller
{
public IActionResult Index(string type)
{
}
}
Run Code Online (Sandbox Code Playgroud)
type在我的过滤器中,我想获取作为查询字符串传递的字符串值。
我的过滤器为:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MyCustomFilter: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//this is always null
string id = filterContext.RouteData.Values["type"].ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么吗?因为我读过一些帖子,并且他们提到了上面的内容来做到这一点。
我有一个HTML表格如下:
<tbody>
<tr ng-repeat="r in targetTable.rows">
<td contenteditable="true" class=""></td>
<td contenteditable="true"
ng-repeat="column in targetTable.columns"
ng-model="r[column.id]"
ng-blur="!r.id? addNewRow(r[column.id], r): undefined">
</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
我使用contenteditable指令使单元格可编辑.
app.directive('contenteditable', ['$sce', function($sce) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
var disable = (attrs.contenteditable === "false") || !Boolean(attrs.contenteditable);
if (!ngModel || disable) return; // do nothing if no ng-model
// Write data to the model
var read = function(value) {
var html = element.html() || (typeof value === "string" ? value …Run Code Online (Sandbox Code Playgroud) contenteditable angularjs angularjs-directive angularjs-ng-model angularjs-forms
我在 .net core Web api 中使用 EPPlus 库。在上述方法中我想验证他上传的Excel。我想知道我的整行是否为空。我有以下代码:
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
//loop through rows and columns
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{
var rowValue = worksheet.Cells[row, col].Value;
//Want to find here if the entire row is empty
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的 rowValue 会告诉我特定单元格是否为空。是否可以检查整行,如果为空则继续到下一行。
我有一个用 .NET Core 编写的 API,并使用 xUnit 来测试它们。
我在 API 中有我的方法:
[HttpDelete("api/{id}")]
public async Task<IActionResult> DeleteUserId(string id)
{
try
{
//deleting from db
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
当 null/empty id 传递给这个方法时,我想编写一个单元测试。
我有我的测试用例:
[Fact]
public void DeleteUserId_Test()
{
//populate db and controller here
var response= _myController.DeleteUserId(""); //trying to pass empty id here
// Assert
Assert.IsType<OkObjectResult>(response);
}
Run Code Online (Sandbox Code Playgroud)
如何检查从我的控制器方法调用返回的状态代码 500。就像是
Assert.Equal(500, response.StatusCode);
Run Code Online (Sandbox Code Playgroud)
在调试时,我可以看到响应具有StatusCode500 的结果返回类型(Microsoft.AspNetCore.Mvc.ObjectResult)。
但是当我尝试这样做时:
response.StatusCode
Run Code Online (Sandbox Code Playgroud)
它抛出我的错误:
“IActionResult”不包含“StatusCode”的定义,并且找不到接受“IActionResult”类型的第一个参数的扩展方法“StatusCode”(您是否缺少 using 指令或程序集引用?)
我该如何解决这个问题?
我正在创建标签:
<tabset class="content-tabset no-margin">
<tab ng-repeat="t in t.values" heading="{{t.name}}" active="t.active">
//other stuff
</tab>
</tabset>
Run Code Online (Sandbox Code Playgroud)
现在在其他内容中我也有按钮,当点击更新行并刷新该部分时.刷新时,它还会重置我当前所在的选项卡.因此,如果我是标签二并单击按钮,面板将刷新,我将返回标签1.如何防止这种情况?
使用.NET Core 2.0 WebApi。
我有一个webapi,其中有许多端点,每个端点都在处理失败时抛出异常并抛出BadRequest。如下:
if(data == null)
{
return BadRequest("Data must not be blank.");
}
Run Code Online (Sandbox Code Playgroud)
现在,由于这些状态代码在我的api中是重复的,因此我正在考虑创建一个Helper方法,该方法会将BadRequest返回到我的API。
因此,我创建了一个静态助手类。但是这里的问题是BadRequest是ControllerBase的一部分,在我的帮助器类中不可用。创建此方法以返回BadRequest的最佳方法是什么。
- 更新 - -
我想要这样的东西:
public static BadRequest GetBadRequestMessage(string message)
{
return BadRequest(message);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
public static BadRequestResult GetBadRequestMessage(string message)
{
return new BadRequestResult(message);
}
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:严重级代码说明项目文件行抑制状态错误CS1729'BadRequestResult'不包含采用1个参数的构造函数
此处使用 .net core api (c#) 和 dynamodb。我的 dbmanager 类为:
public class DbManager<T> : DynamoDBContext, IDynamoDbManager<T> where T : class
{
private DynamoDBOperationConfig _config;
public DbManager(IAmazonDynamoDB client, string tableName) : base(client)
{
_config = new DynamoDBOperationConfig()
{
OverrideTableName = tableName
};
}
public Task<List<T>> GetAsync(IEnumerable<ScanCondition> conditions)
{
return ScanAsync<T>(conditions, _config).GetRemainingAsync();
}
}
public interface IDbManager<T> : IDisposable where T : class
{
Task<List<T>> GetAsync(IEnumerable<ScanCondition> conditions);
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
public class ValuesController : Controller
{
private readonly IDbManager<MyData> _dbManager;
public ValuesController(IDbManager<MyData> dbManager)
{ …Run Code Online (Sandbox Code Playgroud) 这里使用 Angular 6。我有一个页面,其中有大约 2 个子组件。第一个有 2 个输入文本,第二个有一个下拉菜单。我的父组件只有按钮单击。
我知道我可以在我的子组件上使用 Eventemitter 并将值传递给我的父组件,但是如果没有按钮单击事件,我该如何传递子组件的值。我尝试使用 ViewChild ,它可以工作,但在视图完全加载之前会有延迟。我使用 ViewChild 如下:
--家长--
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ChildComponent } from "../child/child.component";
@Component({
selector: 'app-parent',
template: `
Message: {{ message }}
<app-child></app-child>
`,
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child;
constructor() { }
message:string;
ngAfterViewInit() {
this.message = this.child.message
}
}
Run Code Online (Sandbox Code Playgroud)
- 孩子 -
import { Component} from '@angular/core';
@Component({
selector: 'app-child',
template: `
`,
styleUrls: ['./child.component.css']
})
export …Run Code Online (Sandbox Code Playgroud) 我有一个表格,我想限制第一个和最后一个字符。(javascript) 限制是:
例子:
Valid Entries: abc, ABC, Abc123, 123a, Abc_12, Abc_12_3a
Invalid Entries: _abc, abc_, _abc_
Run Code Online (Sandbox Code Playgroud)
我尝试创建我的正则表达式如下:
^(?!\_)(?!_*_$)[a-zA-Z0-9_]+$
Run Code Online (Sandbox Code Playgroud)
这不允许在开头使用下划线,这很好,但在结尾处允许使用下划线,我想限制这一点。
这里缺少任何输入。
谢谢
Valid Entries: abc, ABC, Abc123, 123a, Abc_12, Abc_12_3a
Invalid Entries: _abc, abc_, _abc_
Run Code Online (Sandbox Code Playgroud)
- -更新 - -
我在我的 angularjs 应用程序中使用正则表达式。
我已将指令创建为:
.directive('restrictField', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
var regReplace,
preset = {
'alphanumeric-spl': '\\w_./\s/g',
'alphanumeric-underscore': '\\w_',
'numeric': '0-9',
'alpha-numeric': '\\w'
},
filter = preset[attrs.restrictField] || attrs.restrictField;
ctrl.$parsers.push(function …Run Code Online (Sandbox Code Playgroud) c# ×5
asp.net-core ×3
angular ×2
angularjs ×2
javascript ×2
.net ×1
.net-core ×1
epplus ×1
excel ×1
regex ×1
unit-testing ×1
xunit ×1
xunit.net ×1