假设我有以下对象数组,请将其命名为itemArray;
{
"totalItems": 2,
"items": [
{
"id": 1,
"name": "foo"
},
{
"id": 2,
"name": "bar"
},
]
}
Run Code Online (Sandbox Code Playgroud)
我有一个订阅,返回只有id 2的更新结果.如何在不循环整个数组的情况下更新对象数组?
我想要的是类似下面的例子;
updateUser(user){
this.myservice.getUpdate(user.id)
.subscribe(newitem => {
this.updateArray(newitem);
});
}
updateArray(newitem){
this.itemArray.items[newitem.id].name = newitem.name
}
Run Code Online (Sandbox Code Playgroud)
甚至更好,更换整个物体;
updateArray(newitem){
this.itemArray.items[newitem.id] = newitem
}
Run Code Online (Sandbox Code Playgroud)
但是,此示例根据数组的索引更新数组.那么我如何根据newitem.id进行更新呢?
评论中要求的模板:
<tr *ngFor="let u of itemsArray.items; let i = index">
<td>{{ u.id }}</td>
<td>{{ u.name }}</td>
<td>
<input type="checkbox" checked="u.accepted" [(ngModel)]="itemsArray.items[i].accepted" (ngModelChange)="updateUser(u)">
<label for="singleCheckbox-{{i}}"></label>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud) 我需要检查te后端的身份验证状态,但te代码在完成可观察的返回之前完成.这将导致一个unifined.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.isAuthenticated();
return this.authenticated;
}
isAuthenticated(){
this.loginService.isAuthenticated()
.subscribe(status => this.authenticated = status)
}
Run Code Online (Sandbox Code Playgroud)
我将如何更改此代码,以便我等待observable完成以在代码返回之前获得经过身份验证的状态.
注意:Angular canActivate方法不允许我编写代码,如下所示:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.loginService.isAuthenticated()
.subscribe(status => {
this.authenticated = status;
return this.authenticated;
});
}
Run Code Online (Sandbox Code Playgroud)
这导致以下错误:
类'AuthGuard'错误地实现了接口'CanActivate'.
属性'canActivate'的类型是不兼容的.类型'(路由:ActivatedRouteSnapshot,state:RouterStateSnapshot)=> void'不能分配给类型'(route:ActivatedRouteSnapshot,state:RouterStateSnapshot)=> boolean | 可观察的| 公关......".类型'void'不能赋值为'boolean | 可观察的| 诺言'.
对此错误的解决方案的建议也会有所帮助.
我为每个CustomerProject都有一个重复的表格.重复表单显示了客户的所有项目.如果我有5个项目表单,我想编辑3并添加2并点击提交,一次发布/放置所有内容.
问题是我如何实现这一目标?
表单HTML:
<form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm)">
<!--projects-->
<div formArrayName="projects">
<div *ngFor="let project of myForm.controls.projects.controls; let i=index" class="panel panel-default">
<div class="panel-heading">
<span>Project {{i + 1}}</span>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.projects.controls.length > 1" (click)="removeProject(i)"></span>
</div>
<div class="panel-body" [formGroupName]="i">
<div class="form-group col-xs-6">
<label>Customer</label>
<input type="text" class="form-control" formControlName="customer_id">
<small [hidden]="myForm.controls.projects.controls[i].controls.customer_id.valid" class="text-danger">
Street is required
</small>
</div>
<div class="form-group col-xs-6">
<label>Project</label>
<input type="text" class="form-control" formControlName="project">
</div>
</div>
</div>
</div>
<div class="margin-20">
<a (click)="addProject()" style="cursor: default">
Add another project +
</a>
</div>
<div class="margin-20">
<button …Run Code Online (Sandbox Code Playgroud) 我有一个表单,让我先选择一个产品类型,然后选择产品.由于我有1000多种产品,我使用以下内容来过滤产品列表以提高性能.
我在views.py中有以下内联形式
OrderLineFormSet = inlineformset_factory(OrderHeader, OrderLine, OrderLineForm, extra = 1)
Run Code Online (Sandbox Code Playgroud)
在我的forms.py中,我检查是否已经选择了产品.如果选择了产品,我只会显示具有相同产品类型的产品,以提高负载性能.如果产品为空,它将加载所有产品选项,以便我可以在选择后保存表单.
class OrderLineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(OrderLineForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_show_errors = True
self.helper.error_text_inline = False
if self.instance.product is not None:
self.fields['product'] = forms.ModelChoiceField(queryset=Product.objects.filter(product_type_id=self.instance.product_type_id), required=False)
Run Code Online (Sandbox Code Playgroud)
这导致以下形式

但是,当我更改现有表单上的产品类型(然后使用jQuery更新产品下拉列表)时,我收到错误保存.我知道这是因为选择不是下拉列表中的选项.

我的问题:如何禁用此错误,以便保存我选择的选项,无论原始选项如何.
您将在下面找到此表单的views.py.
def orderline_formset(request, id=None):
OrderLineFormSet = inlineformset_factory(OrderHeader, OrderLine, OrderLineForm, extra = 1)
orderheader = None
orderid = None
orderheaderid = 0
if id:
orderid = OrderHeader.objects.get(pk=id)
if request.POST:
if orderid:
form = OrderHeaderForm(request.POST, instance=orderid)
formset = OrderLineFormSet(request.POST,instance=orderid)
else: …Run Code Online (Sandbox Code Playgroud) 我使用pdfkit和wkhtmltopdf生成pdf文档.当我生成第一个pdf时,一切都很好.当我快速(在5秒内)生成另一个我得到错误[Errno 9]错误的文件描述符.如果我关闭错误(退回浏览器)并再次打开,它将创建pdf.
我的views.py
config = pdfkit.configuration(wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfgen = pdfkit.from_url(url, printname, configuration=config)
pdf = open(printname, 'rb')
response = HttpResponse(pdf.read())
response['Content-Type'] = 'application/pdf'
response['Content-disposition'] = 'attachment ; filename =' + filename
pdf.close()
return response
Run Code Online (Sandbox Code Playgroud)
也许重要的说明:我在IIS8上运行这个站点,当从命令行(python manage.py runserver)运行时,错误不存在.
有关如何处理此错误的任何指导都会很棒.
当使用Facebook营销API时,我想在没有action_attribution_windows 1d_view选项的情况下下载动作.
我目前使用以下设置;
# Ad import fields
fields = [
# Ad meta data
Insights.Field.account_id,
Insights.Field.account_name,
Insights.Field.campaign_id,
Insights.Field.campaign_name,
Insights.Field.adset_id,
Insights.Field.adset_name,
Insights.Field.ad_id,
Insights.Field.ad_name,
Insights.Field.date_start,
Insights.Field.date_stop,
# Ad metrics
Insights.Field.cpc,
Insights.Field.cpm,
Insights.Field.cpp,
Insights.Field.ctr,
Insights.Field.impressions,
Insights.Field.reach,
Insights.Field.spend,
Insights.Field.inline_link_clicks,
Insights.Field.clicks,
Insights.Field.actions
]
# Ad parameters
params_ad = {
'level': Insights.Level.ad,
'limit': limit if limit > 0 else None,
'time_range': {
'since': since,
'until': until
},
'action_attribution_windows': ['28d_click'],
}
# Download data from Facebook
my_insights = my_account.get_insights(fields=fields, params=params_ad)
Run Code Online (Sandbox Code Playgroud)
然而,这将使用1d_view将数据下载为28d_click.我认为当没有给出值时,它默认为1d_view.
我如何禁用1d_view?
我创建了一个 Azure Active Directory 应用程序,我想使用基于角色的安全性。我跟着教程:https : //azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-openidconnect-aspnetcore/
登录有效,我将角色添加到应用程序清单并将角色分配Approver给我自己的帐户。现在我想使用这些角色。
登录后,控制器中的以下工作:
[Authorize]
Run Code Online (Sandbox Code Playgroud)
但是在添加角色时,用户没有被授权:
[Authorize(Roles="Approver")]
Run Code Online (Sandbox Code Playgroud)
以下也返回false:
User.IsInRole("Approver");
Run Code Online (Sandbox Code Playgroud)
似乎角色没有被检索,关于如何将角色功能添加到这个演示项目的任何建议?
我在urls.py中有以下网址
url(r'^inline-formset/$', 'order.views.addrow', {'form_class': OrderedItemForm}, name='addrow'),
Run Code Online (Sandbox Code Playgroud)
现在我想从表单按钮引用此URL但我没有正确的语法.
这有效:
<a href="{% url 'addrow' %}">New Line</a>
Run Code Online (Sandbox Code Playgroud)
这有错误的语法,请协助
<input class="btn btn-success" type="button" value="New Line" onclick="location.href="{% url 'addrow' %}"" />
Run Code Online (Sandbox Code Playgroud) 我需要将星期几作为整数.
如果我在一个月中使用以下内容,mydate.day我希望在工作日也这样做.
我试过,mydate.weekday但这并没有给我整数(星期日0到星期六为6).
有什么建议?
我必须遵循以下数据:
| tid | startdate | enddate |
| 1 | 2016-12-26 12:30 | 2016-12-26 15:30 |
| 2 | 2016-12-26 13:15 | 2016-12-26 15:15 |
Run Code Online (Sandbox Code Playgroud)
我想创建一个带有小时数的结果,然后是日期时间在该小时内的分钟数.
示例结果:
| tid | hour | minutes_in |
| 1 | 12 | 30 |
| 1 | 13 | 60 |
| 1 | 14 | 60 |
| 1 | 15 | 30 |
| 2 | 13 | 45 |
| 2 | 14 | 60 |
| 2 | …Run Code Online (Sandbox Code Playgroud)