本文介绍了角度& Xrm WebApi:无法将结果分配给类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Dynamics CRM构建基本的Angular 8.2 Web资源,但是我无法为类属性分配值.

I'm building a base Angular 8.2 webressource for Dynamics CRM, but I can't manage to assign a value to a class property.

代码如下:

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; ),并且永远不会显示第二个警报.

When debugging (with the .map files), code goes to the first alert ( alert(result.entities[0].fullname); ), show the correct result ("Alexandre Someone"), but don't do anything on the next instruction (this.firstUserFullName = result.entities[0].fullname;), and the second alert is never shown.

推荐答案

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

If you have to access this so you have to use arrow operation (=>) in success callback and in error callback like below

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);
  }
);

这篇关于角度&amp; Xrm WebApi:无法将结果分配给类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:50