如何从AngularJS访问.net功能/属性?

时间:2014-01-15 09:45:26

标签: .net vb.net angularjs

我们有一个托管Web浏览器控件的WinForm应用程序。在此控件中,将显示包含AngularJS的HTML页面。

控制器:

function MyController($scope) {
    $scope.CurrentDialog = window.external;
}

HTML:

<!DOCTYPE HTML>
<html ng-app>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv="MSThemeCompatible" content="yes">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" >
    <script src="angular.min.js"></script>
    <script src="angular.js"></script>
</head>
<body ng-controller="MyController">
    <input type="text" ng-model="CurrentDialog.Text" placeholder="Enter a name here">
    <h1>Hello {{CurrentDialog.Text}}!</h1>

    <span>{{CurrentDialog.Record.Item('Info')}}</span>
</body>
</html>

输入字段和h1工作正常,我们可以从WinForm编辑Text属性而没有任何问题。

但是span-element中的语句不起作用。 “Item”是我们的.NET代码中的公共函数(类Record,它在显示HTML页面之前在WinForm中设置)带有参数。没有参数就可以了。这有什么不对?

AngularJS代码:

{{CurrentDialog.Record.Item}} -> works, output: "Hello world!"
{{CurrentDialog.Record.Item('foobar')}} -> doesn't work, output: "undefined"

VB.NET代码:

Public Function Item() As String
    Return "Hello world!"
End Function

Public Function Item(ByVal Field As String) As String
    Return "Hello world!"
End Function

0 个答案:

没有答案