我想手动编译一些包含HTML的指令.$compile
Angular 2中的等价物是什么?
例如,在Angular 1中,我可以动态编译HTML片段并将其附加到DOM:
var e = angular.element('<div directive></div>');
element.append(e);
$compile(e)($scope);
Run Code Online (Sandbox Code Playgroud) 我想发出一个值.
根据这篇文章,以下内容应该是有效的:
var requestStream = Rx.Observable.just('https://api.github.com/users');
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.just
没有定义.
它被弃用了吗?有什么东西我可以用吗?
我有三个组件:App,Parent和Child:
应用组件
@Component({
selector: 'app',
directives: [Parent,Child],
template: '<parent><child>hi</child><child>there</child></parent>'
})
export class AppComponent {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
父组件
@Component({
selector: 'parent',
template: '<div><h1>Parent</h1><ng-content></ng-content></div>'
})
export class Parent {
@ContentChildren(Child) children: QueryList<Child>;
ngAfterContentInit() {
console.log(this.children);
}
}
Run Code Online (Sandbox Code Playgroud)
子组件
@Component({
selector: 'child',
template: '<div><h1>Child</h1></div>'
})
export class Child {
}
Run Code Online (Sandbox Code Playgroud)
正如您在Parent组件中看到的那样,我尝试使用类型作为选择器@ContentChildren
来获取Child组件列表Child
.但是,这似乎不起作用 - 内容子列表始终未定义.
在该ngAfterContentInit()
方法中,我希望可以填充内容子项.
我错过了什么吗?
[更新]
事实证明,当所有三个组件都在同一个文件中时,问题就存在了(参见我输出内容子项的控制台调试窗口):
如果它们位于单独的文件中,则问题就会消失:
通常,我只会将所有组件放在同一个文件中以供学习.但它让我很好奇.有谁知道为什么行为不同?
我想使用普通JS(而不是typescript)为我的组件指定一个Input属性.
我能找到的唯一文件就是这个(来自Angular2 备忘单):
ng.core.Input(myProperty, myComponent);
//Declares an input property that we can
//update via property binding (e.g. <my-cmp [my-property]="someExpression">).
Run Code Online (Sandbox Code Playgroud)
我在我的组件构造函数中尝试过这个:
.Class({
constructor: function () {
ng.core.Input(this.name, this);
}
});
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用(也没有报告错误).
我究竟做错了什么?
我正在使用gulp/gulp-jasmine/angular来运行我的单元测试.但是,在运行Gulp目标时遇到以下错误:
C:\Projects\website2>gulp test
[01:53:10] Using gulpfile C:\Projects\website2\gulpfile.js
[01:53:10] Starting 'test'...
[01:53:11] Version: webpack 1.4.13
Asset Size Chunks Chunk Names
test.bundle.js 1051728 0 [emitted] test
F
Failures:
1) Exception loading: C:\Projects\website2\scripts\dist\test.bundle.js Error
1.1) ReferenceError: window is not defined
1 spec, 1 failure
Finished in 0.015 seconds
[01:53:11] 'test' errored after 916 ms
[01:53:11] Error in plugin 'gulp-jasmine'
Message:
Tests failed
Run Code Online (Sandbox Code Playgroud)
我相信gulp-jasmine使用PhantomJS(没有触发浏览器窗口).有人可以帮我解决我做错的事吗?我缺少配置设置吗?
这是我的gulpfile.js:
var gulp = require('gulp');
var path = require('path');
var webpack = require('gulp-webpack');
var webpackConfig = require('./webpack.config');
var testWebpackConfig = …
Run Code Online (Sandbox Code Playgroud) 在Angular 1.x中,您可以将ngModel绑定到select控件的模型:
<select ng-model="selectedPerson"
ng-options="person as person.name for person in people">
</select>
Run Code Online (Sandbox Code Playgroud)
选择选项后,selectedPerson
模型将指向person
用户选择的模型.
有没有办法在Angular2中做同样的事情?
我试过以下没有运气:
<select [(ngModel)] = "selectedPerson">
<option *ngFor="#person of people"> {{ person.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
<select [(ngModel)] = "selectedPerson">
<option *ngFor="#person of people" [value]="person"> {{ person.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在第一次尝试中,selectedPerson
模型引用person.name
而不是person
对象.在第二次尝试中,它引用了一个似乎不是JSON对象的对象.
我有什么想法我做错了吗?这甚至可能吗?
如果我已经安装了.NET Core Runtime的最新版本(截至目前,该版本是2.2.3):
https://dotnet.microsoft.com/download/dotnet-core/2.2
该安装是否与.NET Core的先前版本向后兼容?
例如,上述安装是否与针对以下应用的应用兼容:
我意识到.NET运行时可以并排安装。但是,这不能回答我的问题。2.2版的安装是否向后兼容以前的版本,从而不需要额外的运行时?
我有指令做这样的事情:
app.directive('custom', function(){
return {
restrict:'A',
link: function(scope, element){
element.bind('click', function(){
alert('want to prevent this');
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
是的,有必要为这种情况做jQuery-way绑定.
现在,如果满足某些条件,我想停止此事件(单击)传播.
试图做:
$event.stopPropagation();
$event.preventDefault();
Run Code Online (Sandbox Code Playgroud)
但它没有帮助.
这里小提琴 - 例如http://jsfiddle.net/STEVER/5bfkbh7u/
我正在使用Angular@5.1.0遇到AOT构建问题.
错误是:
ERROR in Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'FeatureModule' was called.
Run Code Online (Sandbox Code Playgroud)
feature.module.ts
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([])
],
declarations: [
...
],
exports: [
...
]
})
export class FeatureModule{
static forRoot(items:any[]): ModuleWithProviders {
const routes:Routes = items.map(t=> {
return { path: t.name, component: t.component };
});
return {
ngModule: FeatureModule,
providers: [
provideRoutes(routes)
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
这在非aot构建中成功编译.这似乎只是AOT构建的一个问题.
为什么会发生这种错误?
这是我的代码:
function CustomersController ($scope, $http) {
$http.get('/ci_angular/api/customers/').success(function(customers){
$scope.customers = customers;
});
$scope.addCustomer = function(){
var customer = {
customerName: $scope.customerNameText,
email: $scope.emailText,
address: $scope.addressText,
city: $scope.cityText,
state: $scope.stateText,
postalCode: $scope.postalCodeText,
country: $scope.countryText,
};
$scope.customers.push(customer);
// update 1
$http.post('/ci_angular/api/customers/customer', customer);
$scope.emptyCustomers= {};
$scope.reset = function() {
$scope.customers = angular.copy($scope.emptyCustomers);
};
// update 2
$http.post('/ci_angular/api/customers/customer', customer)
.success(function() {
$scope.emptyCustomers= {};
$scope.customerForm.$setPristine();
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
在成功后,我想重置所有表单字段,但它不起作用,我的表单名称是customerForm,有什么我错过了吗?提前致谢
我正在开始使用 Angular.js,我想知道如何按照以下方式做一些事情(伪代码):
<li ng-repeat="item in items">
<# if(item.dataType == "string") { #>
<input type="text" />
<# } else if(...) { #>
<input type="password" />
<# } #>
</li>
Run Code Online (Sandbox Code Playgroud)
我知道上面的代码不是有棱角的,而且我知道为了简单的处理,我可以使用条件ng-hide
或ng-show
类似的东西。但对于复杂的行为,如果我必须执行各种数据检查和业务逻辑,我如何使用 Angular.js 动态生成 DOM 元素?
我正在尝试在插值字符串中使用变量,但运气不佳。我该怎么做?
var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}
Run Code Online (Sandbox Code Playgroud)
这是我要实现的目标:
Console.WriteLine(t); // PRINTS: hello world mike
Run Code Online (Sandbox Code Playgroud)
这可能吗?
例如,假设我有一个方法:
public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}
Run Code Online (Sandbox Code Playgroud)
用法:
Console.WriteLine(FormatString("The time is {Now}"));
Run Code Online (Sandbox Code Playgroud) angular ×6
angularjs ×4
javascript ×3
.net-core ×2
aot ×1
c# ×1
gulp ×1
gulp-jasmine ×1
jasmine ×1
reset ×1
roslyn ×1
rxjs ×1
typescript ×1