gpt4 book ai didi

javascript - 将网络浏览器控件与 angularjs 一起使用

转载 作者:行者123 更新时间:2023-11-28 01:06:24 29 4
gpt4 key购买 nike

尝试执行以下操作:

(a) 在 winform 中嵌入 Web 浏览器控件。

(b) 通过调用 JS 中的方法将字符串数据从 winform 控件传递到浏览器。

该 JS 方法进一步调用 angularJS Controller 。调用成功。但是, View 中使用的 Controller 不会更新。

下面的片段:

调用侧 C# winform 片段:

string testString = "testing";
webBrowser2.Document.InvokeScript("InvokeJSPassingTestString", new object[] { testString });

HTML 端。

<html ng-app="ManagerApp">
<head>
<meta charset="utf-8" />
<title></title>
<script src="Scripts/angular.js"></script>
<script type="text/javascript">
var angularApp = angular.module('ManagerApp', []);
angularApp.controller('ManagerCtrl', ['$scope', function ($scope) {
$scope.customParams = {};
$scope.updateCustomRequest = function (data) {
$scope.customParams.value = data;
alert("$scope.customParams.value :" + $scope.customParams.value);
};
}]);

function InvokeJSPassingTestString(data) {
var dom_el = document.querySelector('[ng-controller="ManagerCtrl"]')
var ng_el = angular.element(dom_el);
var ng_el_scope = ng_el.scope();
var test = ng_el_scope.updateCustomRequest(data);
}
</script>
</head>
<body ng-controller="ManagerCtrl">
Passed parameter from winform to JS to angularJs is as below:
{{ customParams.value }}
</body>
</>

在上面的代码片段中 - 我收到警报,但 View {{ customParams.value }} 未更新。

欢迎任何意见。

最佳答案

您的代码在 angularjs 摘要周期之外运行,因此您需要在更改数据后启动新的摘要周期

$scope.updateCustomRequest = function (data) {
$scope.$apply(function () {
$scope.customParams.value = data;
alert("$scope.customParams.value :" + $scope.customParams.value);
});
};

How to update bindings

关于javascript - 将网络浏览器控件与 angularjs 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25031979/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com