Reputation: 945
I am trying to bind html dynamically from angular controller
SkillsApp.controller('homeController', function ($scope, $http, $q, $timeout) {
$scope.getAllCategories = function () {
$http({
url: "/Categories/GetAllCategories",
method: 'GET',
}).success(function (response) {
$scope.categoriesList = response;
for (var i = 0; i < $scope.categoriesList.length; i++)
{
var categoryyy = '<li><a href="#" data-filter=".commercial">' + $scope.categoriesList[i].Name + '</a></li>';
$('#Category').append(categoryyy);
}
});
};
Result Html:
<ol class="type" id="Category">
<li><a href="#" data-filter=".commercial">Commercial</a></li>
<li><a href="#" data-filter=".residential">Residential</a></li>
<li><a href="#" data-filter=".commercial">Commercial</a></li>
</ol>
Target Html:
<div class="col-sm-6 col-md-4 col-lg-4 RESIDENTIAL">
<div class="portfolio-item">
<div class="hover-bg">
<a href="img/portfolio/01-large.jpg" title="Project Title" data-lightbox-gallery="gallery1">
<div class="hover-text">
<h4>Project Name</h4>
</div>
<img src="~/img/portfolio/01-small.jpg" class="img-responsive" alt="Project Title">
</a>
</div>
</div>
</div>
But the above code was not binding to the target element.
The same code works perfectly if it is static html code.
Kindly help me where i am doing wrong.
To be more specific : Due to dynamic binding of html the DOM was not able to bind data-filter Is there anyway to refresh DOM objects after the dynamic html binding?
Upvotes: 2
Views: 357
Reputation: 4917
To loop into arrays I recommend to use ng-repeat
directive have a look at ng-repeat documentation
AngularJS is not Jquery see this question to a better understanding on how angularJS works
Also:
Are you setting your app with ng-app="SkillsApp"
Are you setting your controller with ng-controller="homeController"
Do you call getAllCategories()
somewhere ? eg: ng-init="getAllCategories()"
Example
SkillsApp.controller('homeController', function ($scope, $http, $q, $timeout) {
$scope.getAllCategories = function () {
$http({
url: "/Categories/GetAllCategories",
method: 'GET',
}).success(function (response) {
$scope.categoriesList = response;
});
};
<body ng-app="SkillsApp" ng-controller="homeController" ng-init="getAllCategories()" >
<ol class="type">
<li ng-repeat="categorie in categoriesList">
<a href="#">{{categorie.name}}</a>
</li>
</ol>
</body>
Upvotes: 1