Angular & Xrm WebApi:无法将结果分配给 class 变量

Angular & Xrm WebApi: Cannot assign result to class variable

我正在为 Dynamics CRM 构建基础 Angular 8.2 webressource,但我无法设法为 class 属性.

赋值

代码如下:

HTML

<div>
  Hello {{loggedInUserName}} from {{ title }}.<br/>
  <br/>
  The first retrieved user from WebApi was {{ firstUserFullName }}
</div>

和控制器:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'XRM-Angular-Base-App';
  loggedInUserName: String;
  firstUserFullName: String;

  ngOnInit() {
    let context = Xrm.Utility.getGlobalContext();
    this.loggedInUserName = context.userSettings.userName;
    console.log("Logged in user: " + this.loggedInUserName)
    Xrm.WebApi.retrieveMultipleRecords("systemuser", "?$select=firstname,fullname&$filter=firstname eq 'Alexandre'").then(
      function success(result) {
        alert(result.entities[0].fullname);
        this.firstUserFullName = result.entities[0].fullname;
        alert(this.firstUserFullName);
      },
      function (error) {
        alert("Error: " + error.message);
      }
    );

  }
}

调试时(使用 .map 文件),代码进入第一个警报 (alert(result.entities[0].fullname);),显示正确的结果 ("Alexandre Someone"),但在下一个警报中不做任何事情指令 (this.firstUserFullName = result.entities[0].fullname;),第二个警报永远不会显示。

如果您必须访问 this,那么您必须在 success callbackerror callback 中使用 arrow operation (=>),如下所示

Xrm.WebApi.retrieveMultipleRecords("systemuser", "?$select=firstname,fullname&$filter=firstname eq 'Alexandre'").then(
  result => {
    alert(result.entities[0].fullname);
    this.firstUserFullName = result.entities[0].fullname;
    alert(this.firstUserFullName);
  },
  error => {
    alert("Error: " + error.message);
  }
);