Reputation: 604
In my app I'm trying to pass to mvc controller parameters to angular controller. because i want to get data from another api.
MVC Controller
public ActionResult Hotel(long hotelCode, string Destination)
{ return View(model); }
In view i pass model to script. so i can get parameters to angular.
VIEW
<script type="text/javascript">
var data = @Html.Raw(Json.Encode(Model));</script>
AngulerJS Controller
app.controller('ctrl', function ($scope, $q, $window, studentService, filterFilter) {
$scope.datax = $window.data;
$scope.saveSubs = function () {
var sub = {
Des: $scope.datax.HotelCode,
DepartureDate: $scope.datax.Des
};
var saveSubs = APIService.hotelavailability(sub);
saveSubs.then(function (d) {
console.log("Succss");
$scope.respData = d.data.hotels;
}, function (error) {
console.log('Oops! Something went wrong while saving the data.');
alert("Oops! Something went wrong while saving the data.");
});
};
});
Is their any way to call api and bind data to view without passing parameter to angular controller?.
Upvotes: 1
Views: 923
Reputation: 68
Your variable data should be accessible as a global variable, without the need to inject $window.
$scope.datax = data;
If that feels uncomfortable you should be able to access the window variable directly too:
$scope.datax = window.data;
Binding directly in the template could cause problems if you ever update that value, as there's no watch set up on global variables outside the scope of your controller.
Upvotes: 2