Backoffice extension Angular UI doesn't update after fetch call
Creating a simple back-office plugin that has a button on it, and calls an API. For now, I'm just having the API return the current date and I'm attempting to display said date in a variable.
The problem is, when I click the button, nothing seemingly happens, though the console log does show a value coming back from the fetch. However, when I click the button again (or anywhere else on the open area of the screen) the front-end updates with the same value that was in the console.log.
I've also noticed that when I simply mouseover the left-menu pane, the UI also updates as intended. Is there something I'm missing that I need for the UI to update after a fetch call like this?
One more observation, this must have something to do with the fetch code because when I simply just set the $scope.Message1 to new Date() without fetching it from the API, it works fine.
Furthermore, I've tested this without async/await and just using promises and the same incorrect behavior occurs:
angular.module("umbraco").controller("ImportContentController", function ($scope) {
var vm = this;
$scope.Message1 = "";
$scope.Message2 = "";
$scope.testMe = function () {
fetch('/umbraco/backoffice/plugins/welcomedashboard/process')
.then((response) => response.text())
.then((data) => $scope.Message1 = data);
}
});
But i think fetch would work, but you would need to import a polyfill for it to work with older browser versions like IE11 . However i think the async/await part do not work as expected in the angular js version, Umbraco use.
Instead we inject the $http service, that is a wrapper for the HttpClient and is core in angular js.
Then Umbraco got a service named umbRequestHelper, that service we can use to return a promise, so we dont need to handle the responseMessage.
You might want to consider using Umbraco.Sys.ServerVariables.umbracoSettings.umbracoPath instead of /umbraco in case you ever change the umbraco backoffice path.
Backoffice extension Angular UI doesn't update after fetch call
Creating a simple back-office plugin that has a button on it, and calls an API. For now, I'm just having the API return the current date and I'm attempting to display said date in a variable.
The problem is, when I click the button, nothing seemingly happens, though the console log does show a value coming back from the fetch. However, when I click the button again (or anywhere else on the open area of the screen) the front-end updates with the same value that was in the console.log.
I've also noticed that when I simply mouseover the left-menu pane, the UI also updates as intended. Is there something I'm missing that I need for the UI to update after a fetch call like this?
Here's a video showing the issue: https://www.loom.com/share/992a50b0a24d40d4983e7541b95d6a76
Here's the code:
Controller
HTML View
One more observation, this must have something to do with the fetch code because when I simply just set the $scope.Message1 to new Date() without fetching it from the API, it works fine.
Furthermore, I've tested this without async/await and just using promises and the same incorrect behavior occurs:
Hi Travis.
Try this
I will do that, thanks. Does fetch not work in an angular function like I had it?
That did the trick! Can you explain why the initial code I had didn't work?
Hi Travis.
I am not an angular js expert.
But i think fetch would work, but you would need to import a polyfill for it to work with older browser versions like IE11 . However i think the async/await part do not work as expected in the angular js version, Umbraco use.
Instead we inject the $http service, that is a wrapper for the HttpClient and is core in angular js.
Then Umbraco got a service named umbRequestHelper, that service we can use to return a promise, so we dont need to handle the responseMessage.
A little bonus info.
You might want to consider using
Umbraco.Sys.ServerVariables.umbracoSettings.umbracoPath
instead of /umbraco in case you ever change the umbraco backoffice path.Like
Or simply just remove Umbraco and the slash, might also work
Thanks for the help, Bo!
I'm hoping the umbRequestHelper allows for ajax file uploading since the whole point of my plugin is to upload a file :)
is working on a reply...