私は角度が初めてで、角度のコントローラー間で変数を共有する方法を考えています。次のスクリプトを使用しています-
Main.js で:
function MainCntl($scope) {
---code
}
function SearchCtrl($scope, $http) {
$scope.url = 'http://10.0.0.13:9000/processAdHoc';
$scope.errorM = "No results";
$scope.search = function() {
$http.post($scope.url, { "data" : $scope.keywords}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data;
alert('yes');
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
alert('no');
$scope.result = "failed";
});
};
}
Index.html 内
<body ng-controller="MainCntl" >
---code
<div ng-controller="SearchCtrl">
<form class="well form-search">
<div class="ui-widget">
<label for="tags"></label>
<a ng-click="search()"><input type="image" src="../../images/search1.png" class="searchbox_submit" /></a>
<input ng-model="keywords" placeholder="Shadow Search" id="tags" class="input-medium search-query rounded" />
</div>
</form>
</div>
---code
<p ng-model="result">
{{result}}
</p>
</body>
データを送信して応答を受信している ajax ですべてがうまく機能します。私の質問は次のとおりです。
SearchCtrl 関数には、後で Index.html で参照される $scope.result という変数があります。その変数を含む html コードを SearchCtrl コントローラーに挿入すると正常に動作しますが、MainCtrl コントローラーにある場合は動作しません。この変数をコントローラー間で共有するにはどうすればよいですか。
よろしくお願いします