小编Tan*_*eel的帖子

如何在使用切片管道后连接字符串

我是角度新手。我想剪切长度超过 15 个字符的短字符串(例如),然后...在末尾添加。

例如:

姓名:坦泽尔,

角色:实习生

地址: 孟加拉...,

喜欢:C、CPP、...、

我正在使用p-chipsPrimeNg 来显示一些标签。我没有收到任何错误。实际上我什么也没得到,我的网页只是空白。甚至控制台日志也很干净。这是我的代码:

<p-chips [(ngModel)]="tokens">
  <ng-template let-item pTemplate="item">
    {{item | slice:0:15+'...'}}
  </ng-template>
</p-chips>
Run Code Online (Sandbox Code Playgroud)

这是同样的stackblitz 。当我删除时,代码有效+...,但随后没有...看到连接(显然)。请指出我的错误。但是,在一个单独的分支中,我根据这个问题创建了自己的自定义管道:

如何在使用 Typescript 渲染时剪短长字符串

这是代码。

EllipsisPipe.component.ts

import { Pipe } from '@angular/core';
import { SlicePipe } from '@angular/common';

@Pipe({
  name: 'ellipsis'
})
export class EllipsisPipe extends SlicePipe {
  constructor () {
    super();
  }
  transform(value: string, maxLength: number): any {
    const suffix = value && value.length > maxLength ? …
Run Code Online (Sandbox Code Playgroud)

typescript angular-pipe angular

4
推荐指数
1
解决办法
9613
查看次数

为什么单元测试需要 fixture.detectChanges() [Jasmine/Karma]

我正在用 Jasmine 和 Karma 学习单元测试。我的测试用例正在通过,但我不明白一件事。这是我的打字稿:

// array list of objects where each month will have one object i.e. 12 objects (Jan to Dec)
monthsData: Array<{
    monthName: string;
    monthYear: number;
    isInRange: boolean;
    isLowerEdge: boolean;
    isUpperEdge: boolean;
}>;

rangeState: { edge1Exists: boolean; edge2Exists: boolean; edgeIndexes: Array<number> };

initRangeState() {} <---- method which should be called; Not important for this question
Run Code Online (Sandbox Code Playgroud)

这是我在规范文件中的测试用例:

it('should re-initialize range state when reflection is triggered', () => {
    fixture.detectChanges(); <--- why this is required ?
    const rangeState = …
Run Code Online (Sandbox Code Playgroud)

unit-testing karma-jasmine angular

4
推荐指数
1
解决办法
7110
查看次数

在 reactjs 中使用 forEach 读取数组元素

我是 React 的初学者。我有一个非常简单的代码。(还有其他问题似乎与此重复,但它们不是。请在标记重复之前阅读内容,而不仅仅是问题标题。)

import React, { Component } from 'react';
class Likes extends Component {
  render() {
    var music=["linkin park", "led zaplin", "regina spector", "bruno mars"];
    return(
    {
      music.forEach(function (value, index, array) {
          <h1>{value}</h1>
       })
      }
    );
  }
}
export default Likes;
Run Code Online (Sandbox Code Playgroud)

这是正确的方法还是我应该使用state. 我想让它尽可能简单。但我明白了。(见附图)

我错过了什么。还有什么我应该知道的。

PS:我没有任何其他单独的组件或代码。一切都在班级内部。

javascript foreach reactjs

3
推荐指数
1
解决办法
7468
查看次数

如何将一位数整数转换为两位数 [Javascript]

我想转换1,2,3,..9,10,11,12...2401,02,03,...09,10,11,12,...24. 我尝试了这些解决方案。这些都没有解决我的目的:

  1. 如何通过在个位数前加 0 来格式化数字?
  2. 如何在 JavaScript 中输出带前导零的数字 [重复]
  3. 如何用前导零填充值?

我正在处理一个Angular项目,我正在Typescript 中执行此操作。这是我的代码:

monthpikcer.component.ts

monthSlice() {
    let monthStartingIndex = 0;
    monthStartingIndex = ("0" + monthStartingIndex).slice(-2); //error
    let monthEndingIndex = 24;
    for(monthStartingIndex =0; monthStartingIndex < monthEndingIndex; monthStartingIndex++) {
    ...
    } 
}
Run Code Online (Sandbox Code Playgroud)

但是对于 line monthStartingIndex = ("0" + monthStartingIndex).slice(-2),我收到一个错误:

“字符串”类型不能分配给“数字”类型。

我想要它01, 02, 03...10,11,12,13...24。请告诉我如何在不更改变量数据类型的情况下执行此操作,因为稍后我必须对它们进行一些算术运算。

javascript typescript

3
推荐指数
1
解决办法
3272
查看次数

当我们输入 [Angular] 时,如何检测输入字段的变化

我是 Angular 的新手。我正在尝试一些简单的事情。我有一个输入字段。我只想检测变化。每当我在该字段中输入时,我都想调用一些代码。我检查了这些文章:

  1. 文档:Angular - onChanges
  2. Angular 2 中的 OnChanges 和 DoCheck 有什么区别?
  3. 文档:Angular - 生命周期钩子
  4. Stackoverflow:在 Angular 4 中,钩子 ngOnChanges 不会在输入时触发

这是我的代码:

我的component.component.html

<input [(ngModel)]="text">
Run Code Online (Sandbox Code Playgroud)

我的component.component.ts

import { Component, OnInit, DoCheck } from '@angular/core';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit, DoCheck {

  text="hello world";

  constructor() { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges) {
    // changes.prop contains the old and the new value...
  }

  // ngDoCheck() { // TRIED THIS ALSO
  //   console.log("Changes …
Run Code Online (Sandbox Code Playgroud)

onchange typescript angular

3
推荐指数
2
解决办法
5061
查看次数

如何用时刻获取给定年份的最后一天?

我可以硬编码一年的最后一天,因为无论如何它总是 12 月 31 日。有许多使用date, js,的解决方案jquery。但我正在开发一个 Angular 项目,因此我的代码位于typescript. 我的技术主管希望我使用moment. 我不应该对任何日期进行硬编码。是否提供了任何内置方法来获取给moment年份的最后一天。我说给定年份是因为就我而言,给定年份是动态的。它可以随时改变。我正在使用方法。我收到自定义年份错误。我是说:endOf()

const lastDayOfYear = moment().endOf('year')工作正常,但是:

const lastDayOfYear = moment().endOf(2025)给我这个错误:

“2025”类型的参数不可分配给“StartOf”类型的参数

我也尝试过:

  1. const lastDayOfYear = moment().endOf("2025");
  2. year=2025; const lastDayOfYear = moment().endOf(year);

这个方法将如何发挥作用?我已经浏览了整个Moment 文档。或者我应该坚持使用Datejavascript。像这样的东西:

new Date(new Date().getFullYear(), 11, 31);

请提供解决方案。

PS:我只想要指定年份的最后一个日期的MM-DD-YYYY格式。

javascript momentjs typescript

2
推荐指数
1
解决办法
5483
查看次数

为什么我的角度代码无法在屏幕上呈现任何内容

我有一个输入字段。我想在屏幕上实时打印我输入的任何内容。

下面的代码在输入字段中显示“ Hello world”。但不执行任何实时绑定。

档案:app.component.html

<h1>Hello world</h1>
<input type="text">
Run Code Online (Sandbox Code Playgroud)

但是那一刻我补充[(ngModule)]。一切都消失了,甚至是Hello世界。虽然cmd提示说编译成功。

<h1>Hello world</h1>
<input type="text" [(ngModel)]="name">
<p>{{ name }}</p>
Run Code Online (Sandbox Code Playgroud)

我的app.component.ts文件是:

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  name = '';
}
Run Code Online (Sandbox Code Playgroud)

为了方便起见,我还将代码上传到了GitHub

angular

1
推荐指数
1
解决办法
67
查看次数

如何在 ngIf 中赋值

我是角度新手。我正在学习指令。我正在使用pTooltip来自primeng. 但我想根据条件在 pTooltip 中显示文本。

让我们以 Stack Overflow 本身为例。您不能对一个答案投两次赞成票。如果用户没有对该特定文章进行投票,则他应该收到 msg1,否则他应该收到 msg2。在哪里:

msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

文章.组件.html

msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
Run Code Online (Sandbox Code Playgroud)

文章.组件.ts

<button 
  class="btn btn-success"
  *ngIf="allowUpvote ? pTooltip={{msg1}} : pTooltip={{msg2}}
  [disabled]="!allowUpvote">
  UPVOTE
</button>
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

解析器错误:绑定不能包含 [allowUpvote ? 中第 24 列的分配 pTooltip={{msg1}} : pTooltip={{msg2}}]

请给我一些指示。

typescript angular-ng-if angular

1
推荐指数
1
解决办法
5709
查看次数

当新数据从父组件到达时如何重新渲染子组件

我有以下代码。父组件使用指令向子组件发送一些数据@Input

父 HTML

<div>
  <my-timeline
   [data]="data">
  </my-timeline>
</div>
Run Code Online (Sandbox Code Playgroud)

在Parent TS中有一个服务提供数据data

父 TS

data=[];
...
this.service.getData().subscribe(res => {
  res.map(item=>{
    this.data.push({
      ...
     });
   })
})
Run Code Online (Sandbox Code Playgroud)

孩子正在接受它

儿童 TS

@Input data;
eventArray=[];

ngOnInit() {
  console.log("called"); // called only for the first time
  createTimeline(data[0].id); // called only for the first time
}

createTimeline(id) { // called only for the first time
  this.eventArray.push(.....)
}
Run Code Online (Sandbox Code Playgroud)

子 HTML

<p-timeline [value]="eventArray">
...
</p-timeline>
Run Code Online (Sandbox Code Playgroud)

问题是p-timeline继续显示旧事件,即第一次加载的事件。因为父级的后续数据更改不会导致子级中的 ngOnIt 运行。这就是为什么createTimeline再也没有被召唤过。请参与。

primeng angular

1
推荐指数
1
解决办法
4555
查看次数

如何在点击事件中调用Angular组件[Angular]

我不是Angular的专家。我也从互联网上获得了一些答案。特别是这个。我有一个要从中调用用户定义方法的按钮onPress

app.component.html

<div class="dls-menu-item" style="float: right;">
    <button (click)="onPress()"></button>
</div>

<div id="comp-render">
        <-----Here i want that component to be rendered
</div>
Run Code Online (Sandbox Code Playgroud)

app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
    ...
})
export class AnalyticsComponent implements OnInit {
    constructor() {}

    ngOnInit() {}


    onPress() {
        console.log("clicked");
        document.querySelector('#comp-render').innerHTML='<object type="text/html" data="mycomp.html" ></object>';
    }
}
Run Code Online (Sandbox Code Playgroud)

其中mycomp.html是:

<h1>Hello</h1>
<p>This is a custom component.</p>
<button>Click</button>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我该怎么做。

html typescript angular

0
推荐指数
1
解决办法
47
查看次数

如何在 JDBC 中使用 PreparedStatement

我是 JDBC 和 Java 的新手。

我来自 Javascript 和 Typescript 背景。我决定通过创建一个小型基础项目来学习 JDBC 和 Oracle。

我正在使用JDK 8. 我正在关注这个学习材料:TutorialsPoint-PreparedStatement。我发现问题出在我的DataService 上

这是我的DataService课:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataService {
    Connection con;
    PreparedStatement pstmt;

    static String branch_name="";
    static LocalDate branch_created_on;
    static String branch_pulled_from="";

    DataService() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","scott");
        }
        catch(Exception e){ 
            System.out.println(e);
        }
    }

    public void getValue() {
        branch_name=AddNewBranchRecord.branchNameTextField.getText();
        branch_created_on=AddNewBranchRecord.datePicker1.getValue();
        branch_pulled_from=(String) AddNewBranchRecord.combo_box_1.getValue();
    }

    public void putValue() {
        System.out.println("Branch name: "+branch_name);
        System.out.println("Branch created on: …
Run Code Online (Sandbox Code Playgroud)

java jdbc

0
推荐指数
1
解决办法
123
查看次数

如何以反应形式有选择地启用/禁用特定表单控件

我在 Angular 14 中创建了一个响应式表单。我有大约 25 个字段。我希望表单在页面加载时最初处于禁用模式。我正在这样做:

ngOnInit() {
    this.myForm= new FormGroup({
      leNumber: new FormControl(''),
      leName: new FormControl(''),
      leAddrLine1: new FormControl(''),
      leNumberStartDate: new FormControl(''),
      // 21 more fields
    });

    // initially disable the entire form when page loads
    this.myForm.disable();
}
Run Code Online (Sandbox Code Playgroud)

我在用户界面上有一个编辑按钮。单击此按钮后,应启用表单:

<button (click)="enableForm()">Allow Edit</button>

enableForm() {
    this.myForm.enable();
}
Run Code Online (Sandbox Code Playgroud)

要求是,单击“编辑”按钮后,所有字段都应启用,除了leNameleStartDate之外,还有很少类似的字段。如何实现这种选择性启用/禁用字段。请帮忙。

angular

0
推荐指数
1
解决办法
951
查看次数

类型“字符串”不可分配给类型“日期”

我是 TypeScript 的新手。我正在尝试以mm-dd-yyyy格式获取今天的日期,但出现此错误:

类型“字符串”不可分配给类型“日期”

这是我的代码:

initCalendarYear() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1; 
  var yyyy = today.getFullYear();
  if(dd < 10) {
     dd = '0'+dd;
  } 

  if(mm < 10) 
  {
     mm = '0'+mm;
  } 
  today = mm+'-'+dd+'-'+yyyy;
  console.log(today);
}
Run Code Online (Sandbox Code Playgroud)

它应该是直截了当的。没有火箭科学。请纠正我的错误。对于以下几行,我收到此错误:

  1. dd='0'+dd;
  2. mm='0'+mm;
  3. today = mm+'-'+dd+'-'+yyyy;

我正在寻求帮助:

JavaScript : 显示当前日期

typescript angular

-5
推荐指数
1
解决办法
420
查看次数