I'm in the process of dynamically loading a controller based on various values and I'm looking for the most practical way to accomplish this task. Details below.
I'm attempting to access the value of the data-post-id attribute within the controller where it is located.
Let's say there are multiple CommentsController div elements on the same page, each associated with a unique data-post-id.
Any assistance on this matter would be highly appreciated.
Thank you.
JS
app.controller("CommentsController", function($scope, $http) {
$scope.comments = [];
$scope.load = function() {
$http.get("https://myapi.com/comments.json", [post_id: post_id])
.success(function(data) {
$scope.comments = data;
});
};
// Load the data
$scope.load();
});
HTML
<div ng-controller="CommentsController as comments" data-post-id="30">
<div ng-repeat="comment in comments">
{{ comment.body }} - {{ comment.author }}
</div>
</div>