0

I'm trying to run XHR request on URL that controller obtain from template.

I get url like this

Angular code:

ManageControllers.controller('ManageCtrl', ['$scope', '$http',
    function ManageCtrl($scope, $http) {
        $scope.urls = {
            edit : '',
            get : ''
        };

        $http.post($scope.urls.get).success(function(data) {
            $scope.recipe = data;
        });
    }
]);

HTML template:

<div ng-app="ManageApp">
    <div ng-controller="ManageCtrl">
        <input 
            ng-model="urls.save"
            ng-init="
                urls.save = '/save';
            "
        />
        <input
            ng-model="urls.get"
            ng-init="
                urls.get = '/get';
            "
        />
    </div>
</div>

So far Angular reads value from trmplate. But it looks like $http runs before ng-init. So I don't know if it is better to run $http from some function with timeout or there is some proper, angular way of doing it.

Sebastian Piskorski
  • 4,026
  • 3
  • 23
  • 29

1 Answers1

1

You can change your code a bit without the need of a setTimeout :

<input
    ng-model="urls.get"
    ng-init="initGet('/get')"
 />


ManageControllers.controller('ManageCtrl', ['$scope', '$http',
    function ManageCtrl($scope, $http) {
        $scope.urls = {
            edit : '',
            get : ''
        };

        $scope.initGet = function(url) {
            $scope.urls.get = url;
            $http.post($scope.urls.get).success(function(data) {
                $scope.recipe = data;
            });
        };


    }
]);
Jscti
  • 14,096
  • 4
  • 62
  • 87