- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个使用 Babel 和 Angular 1.5.0 的 Angular 全栈项目。
问题是,当我构建一个数组 (this.events = []) 时,我无法将该数组定位到 $onInit() 上,我应该在 $onInit() 上填充要在 ui-calendar 上显示的数据。我在 this.awesomeTesting = [];
上执行此操作所以我需要用 $onInit() 内的 $http.get('/api/calendars/') 中的数据填充 this.events[] 数组,但我不明白为什么我不能定位该数组,这样我就可以填充数据。
我做错了什么?
这是我的代码:
'use strict';
(function() {
class MainController {
constructor($http, $scope, socket, uiCalendarConfig) {
this.$http = $http;
this.socket = socket;
this.awesomeThings = [];
this.awesomeTesting = [];
this.events = [];
this.events.splice(0, this.events.length);
this.eventSources = [this.events];
console.log(this.eventSources);
/* config object */
$scope.uiConfig = {
calendar:{
height: 450,
editable: true,
header:{
left: 'month',
center: 'title',
right: 'today prev,next'
},
dayClick: $scope.alertEventOnClick,
eventDrop: $scope.alertOnDrop,
eventResize: $scope.alertOnResize
}
};
$scope.$on('$destroy', function() {
socket.unsyncUpdates('thing');
})
}
$onInit() {
this.$http.get('/api/things').then(response => {
this.awesomeThings = response.data;
this.socket.syncUpdates('thing', this.awesomeThings);
});
this.$http.get('/api/calendars').then(response => {
this.awesomeTesting = response.data;
this.awesomeTesting.forEach(function (objectItem) {
console.log('displays property in each object: ', objectItem.title);
this.events.push({
title: objectItem.title,
start: objectItem.start
});
});
this.socket.syncUpdates('calendar', this.awesomeTesting);
});
}
addThing() {
if (this.newThing) {
this.$http.post('/api/things', { name: this.newThing });
this.newThing = '';
}
}
deleteThing(thing) {
this.$http.delete('/api/things/' + thing._id);
}
}
angular.module('myApp')
.component('main', {
templateUrl: 'app/main/main.html',
controller: MainController
});
})();
最佳答案
我通过创建一个 for 循环而不是 .forEach() 解决了这个问题,但我仍然想知道答案如何使用 .forEach() 来完成?
for循环的解决方案:
for (var i = 0; i < this.awesomeTesting.length; i++) {
this.events.push({
title: this.awesomeTesting[i].title,
start: this.awesomeTesting[i].start
});
}
关于javascript - 如何使用 Angular-fullstack 从 $onInit() 中定位构造数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934116/
从 angularjs 1.5 迁移到 1.6 的文档说“禁用(默认情况下) Controller 实例上绑定(bind)的预分配,这有助于支持原生 ES6 类。”和“依赖于存在绑定(bind)的初始
所以我一直在阅读 Angular 2,有两种方法可以做同样的事情,但我不知道它是否存在根本区别。 考虑以下问题 构造器 export class MyComponent { myAge: numb
使用 resolver 服务或在 OnInit Hook 中获取数据有什么更好的选择?例如,如果我需要从 3 个不同的来源获取数据到 1 个页面,更好的用户解析器或将代码写入 ngOnInit? 代码
应用程序一直卡在启动画面上的原因是什么?我使用 getx 状态管理。如果 authToken 不为空,它应该转到主页。但是 Controller 类中的 onInit 没有调用。 void main(
我想测试我的 customForm 组件,它使用来自另一个库的组件。首先我想测试我的组件是否初始化了嵌套库组件。让我举个例子: @Component({ selector: 'iris-fiel
无需导入 OnInit 并实现 ngOnInit(){ this.recipies=this.recipeService.getRecipies(); } 这仍然工作正常,没有任何提示,所以我为什
我有以下代码片段 import { Component, OnInit } from "@angular/core"; import { AuthenticationService } from ".
我不断遇到 OnInit 访问同一组件类中的函数的问题。 我的基本设置如下: import {Component, OnInit} from '@angular/core'; ... export c
我有以下代码片段 import { Component, OnInit } from "@angular/core"; import { AuthenticationService } from ".
在 manifest.json 中,我有以下模型定义: { "sap.ui5": { "models": { "SalesInvoices": {
我最近读了this article on smart use of ViewState并且我对 ViewState 中没有不必要的静态数据特别感兴趣。但是,我也很好奇我是否可以为父子下拉列表做同样的事
如何在我的 View 初始化时只运行一次 orderBy 过滤器?我不希望我的列表在运行时重新排序。 最佳答案 改为在 Controller 中使用 orderBy 作为过滤器: app.contr
这个问题在这里已经有了答案: Angular 2 Component Constructor Vs OnInit [duplicate] (3 个答案) 关闭 6 年前。 我只是想弄清楚 Angul
我正在尝试实现 angular 2 onInit 函数,但我在控制台中收到以下错误: Error: ReferenceError: OnInit is not defined(...) 谁能指出我做错
这个问题在这里已经有了答案: Difference between Constructor and ngOnInit (27 个答案) 关闭 6 年前。 如果我希望函数 x 在每次加载组件时都发生,
所以我注意到很多关于这个的话题,但都比较老,而且没有官方的解决方案。也许现在有解决这个问题的方法。但基本上如果你有一个带有用户名输入字段的表单,当页面第一次加载时,如果浏览器自动填充该值(因为你在浏览
在处理 Page_Init 事件或覆盖 Page 的 OnInit 方法之间,使用哪个更好?谢谢。 最佳答案 大约一年前我有这个问题,我决定覆盖而不是 On_X 事件。这是我阅读的文章,介绍了优缺点:
例如,我有两个输入属性: @Input() field1; @Input() field2; 每次初始化 field1 时,我都希望调用 function1();每次我初始化 field2 时,我都希
我的应用中有这个组件: 这是 Controller /组件定义: .component('actionsBar', { controller: 'actionsBarController',
这个问题已经有答案了: How do I invoke onInit() every time I move to a view? [duplicate] (1 个回答) 已关闭 5 年前。 我有两个
我是一名优秀的程序员,十分优秀!