0

I am trying to call function in controller scope from my java code. After lot of search in Google I found this code to call Js function from Java code but I couldn't call my controller function directly.

myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(jsInterface, "JSInterface"); 
myWebView.loadUrl("javascript:"+functionName);

Finally I am able to call Js function from Java. Now I want to call my controller function from other function which is not in controller. I tried below code to call AngulrJs controller function from outside.

var scope = angular.element(document.getElementById("messageList")).scope();
    scope.$apply(function(){
        scope.number = '345';
        scope.message = 'Shan';
        scope.addItem();
    })

But this code resulting below error

Uncaught TypeError: Cannot read property '$apply' of undefined 

I tried both $apply and safeApply, not working. I am new to AngularJs and any suggestion will be helpful to me.

HTML code:

<div id="mainContainer" ng-controller="MainCtrl">
      <ion-nav-view></ion-nav-view>
</div>

<script id="message.html" type="text/ng-template">
      <ion-view title="Message">        
        <ion-content>
        <div id="messageList" ng-controller="MessageCtrl">
          <ion-list>
            <ion-item ng-repeat="item in items">
               {{item.number}} - {{item.message}}!
            </ion-item>
          </ion-list>
          </div>
        </ion-content>
      </ion-view>
    </script>
3
  • work backwards a bit, is document.getElementById("messageList") defined? Commented Jul 20, 2014 at 13:20
  • actually messageList is a part of template. Commented Jul 20, 2014 at 18:17
  • that doesn't answer if it is defined or not when you call your code? Commented Jul 20, 2014 at 18:20

2 Answers 2

1

You are not able to call $apply on your scope var, you can do that like this:

var scope = angular.element(document.getElementById("messageList")).scope().addItem();

This is the way you can access your function.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for ur reply..But its not working, Uncaught TypeError: Cannot read property 'addItem' of undefined
Maybe it's not problem in that part. Can you post more details, or code from the UI side
Code: <div id="mainContainer" ng-controller="MainCtrl"> <ion-nav-view></ion-nav-view> </div> <script id="message.html" type="text/ng-template"> <ion-view title="Message"> <ion-content> <div id="messageList" ng-controller="MessageCtrl"> <ion-list> <ion-item ng-repeat="item in items"> {{item.number}} - {{item.message}}! </ion-item> </ion-list> </div> </ion-content> </ion-view> </script>
I think the destination part not yet loaded. Actually i am trying to update the controller which is available only in template not in a view. It may sound silly question, is it possible to access the controller directly not by DOM element?
currently I am trying to get the controller scope by element. But the element not in view, it is a part of template. So I just want to access the controller function by the Controller name, is it possible?
|
0

If you don't have a target view loaded in time when you call your non-angular function, your contrloller will not be loaded also, so one solution is to have a hidden element with "ng-controller='yourMagicController"' and you can always get that function. Another way is by using $injector. – HelloWorld Jul 20 at 22:59

This approach solved my issue.. Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.