我只是想知道循环遍历表单的所有子元素的最佳方法是什么?我的表单包含input和select元素.
目前我有:
success: function(data) {
$.each(data.details, function(datakey, datavalue) {
$('#new_user_form > input').each(function(key, value) {
if($(this).attr('id') == datakey) {
$(this).val(datavalue);
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
这只循环遍历表单的输入元素,我也想包含select元素:
我试过了:
$('#new_user_form > input, #new_user_form > select').each(function(key, value) {
Run Code Online (Sandbox Code Playgroud)
但这不起作用.有谁知道为什么会发生这种情况?谢谢!
我需要从Javascript/jQuery中的函数返回一个数组,但该函数在数组设置之前返回(因为它是一个AJAX调用).
我被建议使用一个承诺,但我之前没有使用过这些承诺,到目前为止我还没有将它实现到我的代码中.这是我的代码:
mapClass.setLatLng = function(location, clubs) {
document.geoCodeRequestCompleteFlag = 0;
geocoder = new google.maps.Geocoder();
geocoder.geocode({'address' : location}, function(results, status) {
//console.log(results);
if(status === "OK") {
var latLngArray = [];
latLngArray.push(parseFloat(results[0].geometry.location.lat()));
latLngArray.push(parseFloat(results[0].geometry.location.lng()));
var sortedArray = mapClass.calculateDistances(clubs, latLngArray);
return sortedArray;
}
});
}
Run Code Online (Sandbox Code Playgroud)
如您所见,当我返回时,sortedArray变量为空.有没有人有任何想法如何我可以添加阻止代码,以确保在返回之前设置数组变量?谢谢
在将RequireJS合并到应用程序中时,我遇到了使Angular2正确加载的问题.
为简单起见,我在Angular2上使用非常简单的Hello World Javascript教程:https://angular.io/docs/js/latest/quickstart.html
我使用Angular1可以很好地使用这个系统但是我似乎无法使用Angular2复制这个成功.
这是我的index.html文件:
<html>
<head>
<title>Angular 2 QuickStart JS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. Load RequireJS -->
<script type="text/javascript", src="bower_components/requirejs/require.js", data-main="/require.config.js"></script>
</head>
<!-- 3. Display the application -->
<body>
<ireland-product-app>Loading...</ireland-product-app>
</body>
Run Code Online (Sandbox Code Playgroud)
我的require.config.js文件:
require([
'assets/requiredPathsAndShim.js'
], function(requirePathsAndShim) {
require.config({
baseUrl: '/',
paths: requirePathsAndShim.paths,
shim: requirePathsAndShim.shim,
/// Kick start app...
deps: ['app/main']
});
Run Code Online (Sandbox Code Playgroud)
});
我使用requiredPathsAndShim.js文件来加载我看到启动Angular2应用程序所需的所有依赖项.这是文件:
"use strict";
(function(define) {
define([], function() {
return {
waitSeconds : 10000,
paths: {
'shim' : 'node_modules/core-js/client/shim.min',
'zone' : 'node_modules/zone.js/dist/zone',
'Reflect' …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用反应选择来设置选择框的边框颜色,我已经设法做到了这一点,但由于某种原因,当我激活选择框并将鼠标悬停在选项上时,选择框边框颜色的样式默认返回到蓝色。我似乎无法找到 DOM 中我需要定位的位置来更改此设置。问题是这样的:
当我悬停时,会显示正确的(橙色)边框颜色:
但是,当我将鼠标悬停在选项上时,默认的蓝色会显示在选择框周围。我希望它保持橙色:
这是我对选择框的实现。
const customStyles = {
control: (provided: Record<string, unknown>) => ({
...provided,
height: 52,
'&:hover': {
border: '1px solid #ff8b67',
boxShadow: '0px 0px 6px #ff8b67',
},
'&:focus': {
border: '1px solid #ff8b67',
boxShadow: '0px 0px 6px #ff8b67',
},
}),
};
export default function CustomControl(): JSX.Element {
// TODO: select defaultValue by user locale preference possibly
return (
<Select
className="cult-select-box"
styles={customStyles}
defaultValue={countriesJSON[0]}
formatOptionLabel={formatOptionLabel}
options={countriesJSON}
/>
);
Run Code Online (Sandbox Code Playgroud)
谁能明白为什么会发生这种情况?
我构建了一个具有以下文件结构的应用程序:
我正在为此应用程序使用 Typescript 和 Angular2。我在 .ts 文件上有观察者,当有任何变化时,会自动编译打字稿文件。.ts 文件位于/assets/app
文件夹中,转译的 .js 文件的输出目录是 /public 应用程序文件。
这工作正常,但是当 CRUD 处理任何 .html 文件时,我必须直接在/public/app
文件中编辑这些文件,因为它们由于是 .html 而不是 .ts 文件而不会被转换。
我想知道(对于 Angular2 和 Typescript 来说相当新)是否有任何方法可以让 html 视图文件在创建和更新时自动移动到公共文件夹?
这是我当前的 npm 命令,它监视 NodeJS 和 .ts 文件的变化:
"start": "concurrently \"tsc -w \" \"nodemon ./bin/www\"",
Run Code Online (Sandbox Code Playgroud)
谢谢
几个月前我开发了一个使用旧测试版的Angular2应用程序>到目前为止,我正在将另一个应用程序转移到最新版本(RC5版本),没有任何障碍.直到我收到以下错误:
zone.js:484 Unhandled Promise rejection: Template parse errors:
More than one component: ProductComponent,OverlayComponent ("'noscroll': hideBody}">
Run Code Online (Sandbox Code Playgroud)
我有一个子组件产品组件,它作为app-component的子组件包含在内.我在app.module.ts文件中包含了这两个.
我不确定这个错误意味着什么,并且在网上找不到支持.以下是相关文件:
app.module.ts
import './rxjs-extensions';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { ProductComponent } from './components/product.component';
import { OverlayComponent } from './components/overlay-component';
import { ProductService } from './services/product.service';
import { CategoryService } from './services/category.service';
import { Breadcrumb} from "./directives/breadcrumb";
import { CategoryTree } from "./directives/category-tree";
import …
Run Code Online (Sandbox Code Playgroud) 在过去的几周里,我遇到了一个非常奇怪的问题,每次我重新启动我的机器时,我的 MYSQL 安装的 root 密码都会被重置,我必须手动强制更改它。由于它是本地工作环境,因此默认密码应为空。
我正在运行 Ubuntu 16.04 LTS。这只是几周前我所说的,可能与上次 Ubuntu 升级相吻合,但我不能确定。
我曾尝试访问 MYSQL 日志,但这些日志在启动时为空,因此没有为我提供任何信息。
任何人都可以提供有关为什么会发生这种情况的任何帮助或任何其他故障排除方法,因为我不知道从哪里开始。
编辑:要添加,当我在更改后重置 MYSQL 根密码时,我运行以下命令:
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables & followed
mysql -uroot
update mysql.user set authentication_string=password('') where user='root';
Run Code Online (Sandbox Code Playgroud)
谢谢
我在Angular2的一个组件中遇到一个问题,因为“ this”绑定到我的一个组件中的错误上下文。我还有其他组件没有发生此问题,但看不出有什么区别。这是我的代码:
零件:
import { Component, Input } from '@angular/core';
import { FilesService } from "./services/files.service";
@Component({
selector: 'my-app',
moduleId: module.id,
templateUrl:'/app/views/app.html'
})
export class AppComponent {
openFileUploader: boolean = false;
fileUploaderScope:any;
constructor (
private _filesService: FilesService
) {
let self = this;
_filesService.emitter.subscribe(
(response) => {
this.fileUploaderScope = response.filters;
this.openFileUploader = response.open;
},
() => {
},
() => {
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的构造函数中,您可以看到我正在依赖注入文件服务,然后使用“订阅”函数订阅了事件发射器,该事件从该服务在构造器自身内部返回。从下面几行中可以看到,然后我监视事件,并在回调函数中将响应映射到一些本地组件变量:
_filesService.emitter.subscribe(
(response) => {
this.fileUploaderScope = response.filters;
this.openFileUploader = response.open;
},
Run Code Online (Sandbox Code Playgroud)
唯一的问题是“ this”未绑定到正确的上下文。当我在该订阅函数内添加断点时,它说的是“ this”未定义。我正在使用Typescript(ECMA6功能),所以arrow函数具有此绑定的词汇,并且是在构造函数的上下文中定义的,因此“ …
我目前正在构建一个Angular2应用程序,我正在编写测试,但是我遇到了一个在创建测试模块时似乎无法解决的问题.
我创建了一个自定义类,它扩展了Angular2 HTTP模块并用作拦截器.该类负责身份验证逻辑和其他类似的事情.
这是HttpInterceptor类.这用来代替Angular2 HTTP核心类,它只是它的扩展:
import { Injectable, Inject } from "@angular/core";
import { Http, ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response } from "@angular/http";
import { Observable } from "rxjs";
import { AuthService } from "./auth.service";
import { GlobalEventsManager } from "./globalEventsManager.service";
import { messages } from "../helpers/messages";
@Injectable()
export class HttpInterceptor extends Http {
constructor(
backend: ConnectionBackend,
defaultOptions: RequestOptions,
public _authService: AuthService,
public _globalEventsManager: GlobalEventsManager
) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, …
Run Code Online (Sandbox Code Playgroud) 我正在实现一个Javascript类,我无法上班.在初始化类的实例之后,我然后调用该类的方法来生成AJAX请求.然后,需要将AJAX函数"成功"时返回的数据设置为该实例的属性.当我在我的代码中稍后输出this.results变量时,它不应该是空的.这是我的代码:
//Individual Member Class
function GetReports(options) {
this.options = options;
this.results = {};
}
GetReports.prototype.getResults = function() {
jQuery.ajax({
type : 'post',
dataType : 'json',
url : 'reporting/getStaffMemberReports',
async : false,
data : options,
success : function(data) {
this.results = data;
setResult(this.results);
}
});
}
GetReports.prototype.returnResults = function(type) {
if(type === "JSON") {
JSON.stringify(this.results);
} else if (type === "serialize") {
this.results.serialize();
}
return this.results;
};
GetReports.prototype.setResult = function(data) {
this.results = data;
};
Run Code Online (Sandbox Code Playgroud)
我的代码创建了'class'的实例:
var jsonString = <?php echo …
Run Code Online (Sandbox Code Playgroud)