2

I'm calling the function from laravel blade template but it is sending multiple requests that's make the browser unresponsive Here is my code

 **Laravel View**
  <div ng-controller="HotelsListController as listCntrl">
     <section class="section section-sec top-hotels hotels-sec">
        <div class="container">
           {{ listCntrl.hotelsRequests() }}
         </div>
      </section>  
   </div>

**Angular JS **

   (function(){

       angular
        .module('app')
        .controller('HotelsListController',hotelsListController);

        hotelsListController.$inject = ['$http'];

        function hotelsListController($http){

           var vm =  this;
           vm.hotelsRequests = getHotelData;


        function getHotelData(){
           $http.get('/hotels/getHotelsRequest').then(function(response){
              console.log(response);
           });
      }
   }
})();

1 Answer 1

1

Here you can use ng-init directive of AngularJS

 <div ng-controller="HotelsListController as listCntrl">
 <section class="section section-sec top-hotels hotels-sec" ng-init="hotelsRequests()">
    <div class="container">
     </div>
  </section>  

As it will call your function getHotelData().

Happy coding :)

Sign up to request clarification or add additional context in comments.

4 Comments

so i cannot use directly into my view like this {{ listCntrl.hotelsRequests() }}
It's a safe way to load function using ng-init. it will get render when you initialize page. an additional suggestion, you can write an angularJS part more feasible way. i.e. w3schools.com/angular/angular_controllers.asp
This is not what ng-init is intended for. Should just call the method directly in controller and render the response in view by assigning to a controller property
Also should be listCntrl.hotelsRequests() since OP is using controller as syntax and the function itself needs to be modified as does what gets rendered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.