Using Angular 4, I have a html template and I want a selection box with two options. One of those options should be pre-selected by default.
<select name="rate" #rate="ngModel" ngModel required>
<option selected value="hr">hr</option>
<option value="yr">yr</option>
</select>
Run Code Online (Sandbox Code Playgroud)
I assign #rate="ngModel"
so that I can reference the value somewhere else in the template, in a conditional statement or with interpolation {{rate.value}}
. For that to work I simply add ngModel
to the tag. I'm not binding to anything in the …
角度版本:6.1
我正在尝试使用Kara Erickson 所称的 Composite ControlValueAccessor 来实现自定义控件,如演示文稿Angular Forms – Kara Erickson – AngularConnect 2017 中所示。控件的值应设置为两个子输入值之一。
这个问题writeValue()
似乎只在最初被调用,而不是在进一步的价值变化上。
这是一个stackblitz 演示。
<form #f="ngForm" (ngSubmit)="f.form.valid && Submit(f)">
<confirm-password name='password' ngModel #pwd='ngModel'></confirm-password>
<div>{{pwd.status}}</div>
</form>
Run Code Online (Sandbox Code Playgroud)
<div [formGroup]='pass'>
<label>Password:
<input type="password" formControlName='pwd1' (blur)='onTouched()'>
</label>
<div *ngIf="controlDir.touched && controlDir.control.errors?.required"
class="error">Password is required.</div>
<label>Please re-enter password:
<input type="password" formControlName='pwd2' (blur)='onTouched()'>
</label>
<div class='error' *ngIf='controlDir.touched && controlDir.control.errors?.notEqual &&
!controlDir.errors?.required'>Passwords do not match.</div>
</div>
Run Code Online (Sandbox Code Playgroud)
import { Component, Self, OnInit, OnDestroy …
Run Code Online (Sandbox Code Playgroud) 目标是当用户单击按钮转到下一页时,阻止用户继续,并在页面部分填写但无效时显示错误指示器。
在模板驱动的表单中,我有几个使用NgModelGroup
属性来表示表单中的“页面”的容器元素。在我的组件中,我想引用这个集合,以便我可以通过索引访问它们。由于我无法获取此集合(@AJT_82 在下面评论原因),这就是我的方法:
我创建了一个类来保存有关页面的信息。
export class Page
{
title: string;
hasError: boolean; //should the page display an error indicator
}
Run Code Online (Sandbox Code Playgroud)
pages: Page[]
在我的组件中,我在 ngOnInit 中填充一个数组
ngOnInit()
{
this.pages = [{title: "Page 1", hasError: false},
{title: "Page 2", hasError: false},
{title: "Page 3", hasError: false}]
}
Run Code Online (Sandbox Code Playgroud)
@DeborahK 在她的回答中给了我
this.form.form.get('pg1'))
Run Code Online (Sandbox Code Playgroud)
获取ngModelGroup
名为 'pg' + currentPg+1 的个体(+1 来匹配视图,因为数组从 0 开始),然后我可以在单击事件中使用它,该事件将导致 A)转到下一页或 B)设置属性hasError
设置为 true 并且不会进入下一页。
let p = this.form.form.get("pg"+ (this.currentPg+1));
//if the page is partially filled out but not …
Run Code Online (Sandbox Code Playgroud) 在 VS Code 中,我有一个少于 50 个文件的 Vue 项目,但在运行开发服务器 VS Code 时抛出Error: ENOSPC: System limit for number of file watchers reached
.
我的VS Code Watcher Exclude 设置具有以下忽略模式。
**/.git/objects/**
**/.git/subtree-cache/**
**/node_modules/**
Run Code Online (Sandbox Code Playgroud)
我试图找出哪些文件可能使我超过了机器上的max_user_watches限制,即 8192。
通过用户“mosvy”的 StackExchange回答和另一个用户“oligofren”从该回答派生的脚本,我发现以下来源可能导致这种情况。
| Watchers | Source |
| -------- | ------ |
| 4033 | /usr/share/code/code --max-old-space-size=3072 /usr/share/code/resources/app/extensions/node_modules/typescript/lib/tsse |
| 3889 | /usr/share/code/code /usr/share/code/resources/app/extensions/node_modules/typescript/lib/typingsInstaller.js --globalTy |
| 99 | /usr/share/code/code /usr/share/code/resources/app/out/bootstrap-fork --type=watcherService |
Run Code Online (Sandbox Code Playgroud)
我不确定为什么要观看其中任何一个,但前两个似乎忽略了 VS Code 的 …
如何设置 Webpack 4.5 将具有自己的 css 文件的 html 文件注入到主index.html中,而无需 Angular、React 等框架,并且我想要注入的每个 html 文件都没有 javaScript 文件?
我已经创建了单独的 html 文件并将它们注入到index.html中,但还没有弄清楚如何使这些 html 模板拥有自己的本地范围的 css 文件。
在home.html内部,我希望能够像平常一样
使用home.css
中的类(即不喜欢<div className={styles.foo}>
,这是我发现的最接近的)
<div class=foo></div>
<div>
<p class=bar></p>
</div>
Run Code Online (Sandbox Code Playgroud)
.foo {background: red;}
.bar {background: green;}
Run Code Online (Sandbox Code Playgroud)
并自动将其范围限定为本地范围,以便在我的index.html中我可以注入home.html而不会发生CSS冲突,并且每个.html文件都不需要它自己的.js文件。
<!doctype html>
<html lang="en">
<head>
<title>Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="styles.css" rel="stylesheet">
</head>
<body>
<div id="pages">
<!-- inject page templates below -->
<section id="home" …
Run Code Online (Sandbox Code Playgroud) 使用Webpack 4以及HtmlWebpackPlugin和html-loader,我正在尝试:
但是,当webpack.config.js中存在html-loader时,此方法不起作用。如果我可以使用首选的相同语法,但是我已经尝试使用${ }
title标签,但是出现构建错误htmlWebpackPlugin undefined
。
<!doctype html>
<html>
<head>
<title><%= htmlWebpackPlugin.options.title %></title> <!-- this doesn't work -->
<!-- <title>${htmlWebpackPlugin.options.title}</title> //also doesn't work -->
</head>
<body>
<section>${require("./path/to/other.html")}</section> <!-- this works fine -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
entry: './src/main.js',
devServer: {
contentBase: './src',
port: 4200,
open: true
},
plugins: [
new HtmlWebpackPlugin({
hash: true,
template: './src/index.html'},
title: …
Run Code Online (Sandbox Code Playgroud) 在 Angular 4 中,是否可以在单击事件中执行代码而无需为其编写函数?例如
<button (click)="bool=true; console.log("print"); Function()">Button1</button>
Run Code Online (Sandbox Code Playgroud)
阅读了一些内容后,这可能不可能的原因是 html 元素(在本例中为按钮)属于 typescript 组件,而不是 html 模板,因此您不会在函数之外有代码。但是......我不确定这是否接近正确的理解。
这篇文章 console.log notworking in Angular2 Component (Typescript)回答了 console.log 的问题,但在其他情况下仍然需要一些解释,例如
<button (click)="bool=true">Button1</button>
接受的答案解释了其局限性。
我知道这已经多次被问到类似的差异,但是我在 C#(Unity3D) 中按位运算的输出遇到了问题。
我正在尝试进行位反转排列,即为了在Cooley-Tukey FFT 算法中使用而获得整数(或无符号整数,任一)的位反转顺序。所以如果我有 0, 1, 2, 3 - 我想以 0, 2, 1, 3 结束,如果我有 0, 1, 2, 3, 4, 5, 6, 7 - 我应该得到 0, 4、2、6、1、5、3、7。
我尝试了一些在网上找到的位反转算法,例如这个:
public uint ReverseBits(uint n)
{
n = (n >> 1) & 0x55555555 | (n << 1) & 0xaaaaaaaa;
n = (n >> 2) & 0x33333333 | (n << 2) & 0xcccccccc;
n = (n >> 4) & 0x0f0f0f0f | (n << 4) & 0xf0f0f0f0;
n = …
Run Code Online (Sandbox Code Playgroud) 我正在使用mySQL,并且已将SQL语句分配给PHP中的变量。稍后,我有一个HTML <p>
元素,我想在其中调用SQL语句并将结果插入到字符串中。因此,如果中有一项AppsToBeApproved
,则结果<p>
内容应为“您有1个新应用程序”。
“ appID”是每个项目的自动递增主键
<?php
$newAppsCount = "COUNT(appID) FROM AppsToBeApproved";
?>
<p><?php echo "You have ".$newAppsCount." new applications."?></p>
Run Code Online (Sandbox Code Playgroud)
这段代码的结果是:
"You have SELECT * FROM AppsToBeApproved new applications."
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?