Need a suggestion on whether i can use attr
function to asign template using data-ng-include
on a div
-
Code in main.js
-
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
$scope.Values = [{"name":"John", "category":1},
{"name":"Jack", "category":1},
{"name":"Alina", "category":2},
{"name":"Joseph", "category":2}]
$scope.categoryMatch = function (item) {
return item.category == $scope.currentCategory;
}
$scope.currentCategory = 1;
$("#div1").attr("data-ng-include", "'htmlpage1.html'");
//above attr function does not assign template to div1
$scope.currentCategory = 2;
$("#div1").attr("data-ng-include", "'htmlpage1.html'");
//above attr function does not assign template to div2
});
htmlpage1.html
:
<div ng-repeat="value in Values | filter:categoryMatch">
...do stuff...
</div>
index.html
:
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body ng-controller="MyCtrl">
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
Can you please let me know if this is the correct way of doing this or whether i am missing anything? basically i need attr
function to work in my code.