서비스에서 반환 한 데이터를 $ scope 속성에 할당하려고합니다. 어떻게 든 제대로 작동하지 않습니다. 서비스 메소드는 $ http.get을 통해 데이터를 올바르게 가져 오지만 컨트롤러의 $ scope에는 할당되지 않습니다.
app.service('StoreService', ['$http', function ($http) {
this.getStoreNamesService = function() {
console.log('getStoreNames called');
$http.get('http://localhost:8080/storys')
.success(function (response, status) {
console.log(response);
return response;
})
};
}]);
app.controller('ItemFormController', ['$scope', '$http', '$mdDialog', 'itemService', 'StoreService', function ($scope, $http, $mdDialog, itemService, StoreService) {
$scope.storeNames = StoreService.getStoreNamesService();
}]);
응답을 인쇄하면 올바른 데이터가 제공됩니다. 그러나 $ scope.storeNames를 인쇄 할 때 뷰에는 정의되지 않은 데이터도 없습니다.
app.js :
var app = angular.module('BlankApp', ['ngMaterial', 'ngRoute'])
.config(function($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('teal')
.accentPalette('red')
.warnPalette('red');
});
app.config(function ($routeProvider) {
$routeProvider
.when('/addItem', {
templateUrl: 'templates/addItemForm.html',
controller: 'ItemFormController'
})
.when('/', {
templateUrl: 'templates/first.html'
})
.when('/store', {
templateUrl: 'templates/itemsInStore.html',
controller: 'StoreController'
})
.when('/item/:itemId', {
templateUrl: 'templates/itemView.html',
controller: 'ItemController'
})
.otherwise({
template: '<h1>otherwise template</h1>'
})
});
스크립트 태그의 순서 :
<!-- Angular Material requires Angular.js Libraries -->
<script src="js/angular-1.5.8/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-route.min.js"></script>
<!-- Angular Material Library -->
<script src="js/AngularMaterial/angular-material.js"></script>
<!-- Your application bootstrap -->
<script src="js/app.js"></script>
<script src="js/service/itemService.js"></script>
<script src="js/service/StoreService.js"></script>
<script src="js/controller/testController.js"></script>
<script src="js/controller/SideNavController.js"></script>
<script src="js/controller/ItemFormController.js"></script>
<script src="js/controller/sampleController.js"></script>
<script src="js/controller/ItemController.js"></script>
AJAX 호출이 완료되지 않고 함수가 befote 반환하기 때문에이 일어나고있다. $ scope를 매개 변수로 사용하여 콜백을 보내거나 Promise를 사용하십시오 – bugwheels94