Davide
Davide

Reputation: 153

Call angular js method by silverlight control

I've my Web page structured with AngularJs that hosting a Silverlight Control. Now I need to call a function placed in a controller angular from silverlight. This is my controller angular code sample:

    var myModule= angular.module('MyModule', []);

    myModule.controller('MainController', ['$rootScope', '$scope', '$http', function ($rootScope, $scope, $http) {

        var _self = this;
        _self.testMethodForSilverlight = function (message)
        {
            alert("message: " + message);
        }; 
}]);

and this is the code in my Silverlight viewModel:

public class ViewModel : ViewModelBase, INotifyDataErrorInfo
    { 
      public void CallAngularMethod()
      {
           HtmlPage.Window.Invoke("testMethodForSilverlight ", new[] { "Testing" });
      }
    }

Unfortunately when I call the method the error returned is ("Failed to invoke: testMethodForSilverlight" ). Does anyone have any idea?

Upvotes: 0

Views: 413

Answers (1)

Krishna Veeramachaneni
Krishna Veeramachaneni

Reputation: 2141

You can try writing your custom Javascript code and do something like this

<script type="text/javascript">
 function callAngularFunc(message) {
    angular.element($("#elementID")).scope().testMethodForSilverlight(message);
 }
</script>

Here, elementID will be the DOM element that the angular controller is bound to.

There is more info related accessing angular data from outside in this post.

Then, in your silverlight code, make the call like this

public class ViewModel : ViewModelBase, INotifyDataErrorInfo
    { 
      public void CallAngularMethod()
      {
           HtmlPage.Window.Invoke("callAngularFunc", new[] { "Testing" });
      }
    }

Upvotes: 1

Related Questions