如何在angularjs中显示Div标签中的错误?

时间:2022-05-13 06:45:37

I have a div tag as a placeholder in my view to render any errors received from a service call in angular js.

我在视图中有一个div标签作为占位符来呈现从角度js中的服务调用收到的任何错误。

<div id="error" class="row"></div>

The function in controller is as below which makes a service call and then tries to update error tag in view.

控制器中的功能如下所示,它进行服务调用,然后尝试更新视图中的错误标记。

$scope.send = function () {

$("error").text("");

var payment = {Id: 1, Amount: 100};

service.submit(payment, function (response) {

 var pmtResponse = Process(response);

 if (pmtResponse.errorMsg != null) {
      alert(pmtResponse.errorMsg);
      $("error").text(pmtResponse.errorMsg);
     }

 }

}

It shows an alert message when there is errorMsg but it does not do anything with updating error div tag. Is there any angular way of doing it? Or how can I make it work with jQuery?

它会在出现errorMsg时显示警告消息,但它不会更新错误div标记。这样做有什么棱角分明的方式吗?或者我如何使它与jQuery一起使用?

Thank you!

谢谢!

2 个解决方案

#1


4  

rather than $("error").text(pmtResponse.errorMsg); (no need for jquery).

而不是$(“错误”)。text(pmtResponse.errorMsg); (不需要jquery)。

Your div:

你的div:

<div id="error" class="row" ng-show="errorMsg">{{ errorMsg }}</div>

{{errorMsg}}

and your JS:

和你的JS:

if (pmtResponse.errorMsg != null)
{
  alert(pmtResponse.errorMsg);
  $scope.errorMsg = pmtResponse.errorMsg;
}

That would be the "angular way" of doing this

这将是这样做的“有条不紊的方式”

#2


1  

Use this because you are accessing div by id

使用此方法是因为您正在通过id访问div

$("#error").text("");

$("#error").text(pmtResponse.errorMsg);

#1


4  

rather than $("error").text(pmtResponse.errorMsg); (no need for jquery).

而不是$(“错误”)。text(pmtResponse.errorMsg); (不需要jquery)。

Your div:

你的div:

<div id="error" class="row" ng-show="errorMsg">{{ errorMsg }}</div>

{{errorMsg}}

and your JS:

和你的JS:

if (pmtResponse.errorMsg != null)
{
  alert(pmtResponse.errorMsg);
  $scope.errorMsg = pmtResponse.errorMsg;
}

That would be the "angular way" of doing this

这将是这样做的“有条不紊的方式”

#2


1  

Use this because you are accessing div by id

使用此方法是因为您正在通过id访问div

$("#error").text("");

$("#error").text(pmtResponse.errorMsg);