템플릿 (index.html
), 앱 정의(), 컨트롤러 정의 (controllers.js
) 및 호스팅 페이지 (host.jsp
)로 구성된 다음 AngularJS 애플리케이션이 있습니다. 다음
코드는 :
<div class="container-fluid" ng-app="facetedSearch">
<div class="row-fluid" ng-view>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="/resources/js/page/search/app.js"></script>
<script src="/resources/js/page/search/controllers.js"></script>
가
angular.module('MyApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {templateUrl:'/index.html', controller: MyController}).
otherwise({redirectTo: '/'});
}]);
controller.js
var MyController=['$scope','$http','$location',function ($scope, $http, $location) {
//do some fancy stuff
if($scope.myAttribute===undefined){
$scope.myAttribute=someDataFromHttpRequest;
}
$location.search(someParameters);
}];
index.html 또는 호스트 app.js
search.jsp. jsp는 간결함과 무관 함으로 표시되지 않습니다. vance.
컨트롤러는 Ajax 요청에서 일부 데이터를 가져 와서 다시 요청하지 않도록 컨트롤러의 일부를 $scope
에 저장하고 사용자에게 보여주고 입력을 기다립니다. 사용자가보기에서 일부 데이터를 선택하면 선택 사항 변경 사항을 반영하도록 URL 쿼리 부분을 업데이트합니다. 하지만 데이터가 $scope
에서 사용 가능한지 확인하여 후속 Ajax 요청을 피하기를 원합니다.
내가 직면 한 문제는 $scope.myAttribute
이 항상 정의되지 않는다는 것입니다. 모든 요청에 대해 재설정됩니다. 나는 AngularJS를 오용하고 있다고 생각합니다. 어떤 단서?