次のコードがあります。
app.controller('MatrixExpertCtrl', function($scope,$http){
$scope.PassedMatrix=[];
$scope.GetMatrixFromServer=function(){
$http.get("http://localhost:3000/getmatrixfromdb").success(function(resp){
alert("The matrix grabbed from the server is: " + resp[0].ans);
$scope.PassedMatrix.push(resp[0].ans);
});
};
$scope.DispSize=function(){
alert("testing");
alert("The size is "+$scope.PassedMatrix[0].size) ;
};
//$scope.GetMatrixFromServer();
});
さて、HTML で次のようなものがあるとします。
<div class="col-sm-3 col-md-3 col-lg-3">
<div class="text-center">
<h3>Example Survey</h3>
<p>example paragrah</p>
<p>More random text</p>
<p>ending the paragraphs</p>
<button id="updmat" ng-click="DispSize();" type="button" class="btn btn-default">Updates</button>
</div>
//Much more code
<div id="body2">
<div class="col-sm-6 col-md-6 col-lg-6" style="background-color:#ecf0f1;">
<div ng-controller="MatrixExpertCtrl" ng-app="app" data-ng-init="GetMatrixFromServer()">
<div class="text-center">
これで意味:
同じコントローラーのスコープ外から、コントローラー内で定義されている関数を呼び出すことは可能ですか?
これが必要なのは、関数が非常に単純な方法でコントローラーが所有する共有オブジェクトを操作しているためです (たとえば、ボタンをクリックすると、特定の要素の色が変わります)。
私はこれを機能させるのに問題があります。
一部のデータ構造をグローバルとして宣言すると、この問題を解決するのに役立つと思いますが、悪い習慣であることに加えて、将来さらに問題が発生する可能性があるため、それを避けたいと思います。