JSONファイルから名前を読み取ろうとしています。私はまず唯一のコントローラを使用してサービスを作成しませんでした。しかしビジネスロジックをサービスに移しました。 $ scope.mydataをサービスからコントローラに返す方法については、明確なアイデアは得られていません。 出力が得られません。ありがとうございました。AngularJSのサービスを使用してオブジェクト値をコントローラに返す
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Add angularjs -->
<!-- Add controller script-->
<script type="text/javascript">
var app = angular.module('x2js', []);
app.controller('ctrl',
['$scope','myService',function($scope,myService){
$scope.data = function(){
myService.getData()
}
}]);
app.service('myService', ['$http','$log', function($http,$log){
this.getData = function(){
$http({
url:"student.json",
method:"GET",
dataType: 'json',
contentType: "application/json"
}).then(function(response){
$scope.myData = response.data.records;
$log.info($scope.post);
return($scope.myData);
},function(response){
$log.error("Error occured");
});
}
}])
</script>
</head>
<body ng-app="x2js">
<div ng-controller="ctrl">
<table>
<tr>
<td ng-repeat="x in Data">{{x.Name}}</td>
</tr>
</table>
</div>
</body>
</html>
まだ出力がありません。 : –
@AmitPhartiyalあなたはhtmlと 'data'変数に割り当てられたコントローラの値に' Data'を使用しました。 –
実際には彼の例のmyDataです – pegla