2014-11-04 3 views
1

나는 도움이 필요한 문제가 있습니다. 프론트 엔드에서 각도 컨트롤러를 사용하여 안드로이드 함수를 호출하려고하는데, 제대로 작동하지 않습니다.AngularJS 컨트롤러에서 Android 기능 호출

MainActivity :

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mWebView = (WebView)findViewById(R.id.activity_main_webview); 
    mWebView.setWebViewClient(new CustomWebViewClient()); 
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    mWebView.addJavascriptInterface(new AngularJSInterface(this), "Android"); 
    mWebView.loadUrl("http://192.168.70.101:3000/"); 

} 

AngularJSInterface :

public class AngularJSInterface { 

Context mContext; 

AngularJSInterface(Context c) { 
    mContext = c; 
} 

public void showToast(String toast) { 
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
} 

}

각도 컨트롤러 :

angular.module('app').controller('ComplaintCtrl', function ($scope, $http, complaintService) { 

    $scope.showToast = function(toast) { 
     Android.showToast(toast); 
     console.log("toast"); 
} 

});

html로 : 콘솔에서

<button label="toast" ng-click="showToast('Visar toast från angularJS')">toast</button> 

오류 :

ReferenceError: Android is not defined 
at Scope.$scope.showToast (http://localhost:3000/scripts/controllers/addcomplaint.js:34:3) 
at http://localhost:3000/bower_components/angular/angular.js:10567:21 
at http://localhost:3000/bower_components/angular-touch/angular-touch.js:438:9 
at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:12412:28) 
at Scope.$apply (http://localhost:3000/bower_components/angular/angular.js:12510:23) 
at HTMLButtonElement.<anonymous> (http://localhost:3000/bower_components/angular-touch/angular-touch.js:437:13) 
at HTMLButtonElement.jQuery.event.dispatch (http://localhost:3000/bower_components/jquery/dist/jquery.js:4409:9) 
at HTMLButtonElement.elemData.handle (http://localhost:3000/bower_components/jquery/dist/jquery.js:4095:28) 

이 내가 이런 종류의 물건을 사용하고 처음으로 내가 실제로 작업을 수행하는 방법을 아주 잘 모르겠어요 . 인터넷 검색으로 답변을 찾으려고했지만 행운이 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

0

토스트 기능을 정적으로 변경하고 직접 호출하거나 직접 인스턴스화하십시오. 그리고 AngularJSInterface을 작성한 대신 Android이라는 클래스가 없습니다. Android.showToastAngularJSInterface.showToast으로 변경하십시오 (정적 인 경우).

+0

showToast 메서드를 static으로 변경하고 Angular.showToast()를 AngularJSInterface로 바꿨습니다. showToast(), 여전히 동일한 오류가 발생하지만 AngularJSInterface는 정의되지 않았습니다. 얼마나 정확하게 인스턴스화 할 수 있습니까? – CobraAn

+0

AngularJSInterface myInterface = 새로운 AngularJSInterface(); myInterface.showToast(); –

관련 문제