J. Petersen J. Petersen - 3 months ago 12
AngularJS Question

Send ID over to angularjs - so the true content appears

I am storing an Id in a hidden field; it can also be another number such as 2, 3, 4 or 59. It must take the Id coming from the hidden field and must send it over to my opgaver.js file. where it will then download the content.

I'm stuck on how to send the Id to the opgaver.js file.

index.html

<div class="col-md-12" ng-app="Opgaver" ng-controller="OpgaverCheck">
<input type="hidden" value="1" ng-model="Id" />
<div ng-repeat="Value in Newslist">

</div>
</div>


Opgaver.js

var app = angular.module('Opgaver', []);
app.controller('OpgaverCheck', function ($scope, $http) {

//GET
var url = "/opgaver/kategori/"; //Id HERE//

$http.get(url).success( function(response) {
$scope.Newslist = response;
});
});


The problem is: How to get my Id over to opgaver.js so content can appear there.

Answer

Your HTML should be like

<input type="text" ng-init="Id='1'"  ng-model="Id" />

And inside your controller:

$scope.$watch("Id", function() {
  var url = "/opgaver/kategori/" + $scope.Id;
    $http.get(url).success( function(response) {
      $scope.Newslist = response; 
    });
});