I have a div where I want to put checkboxes with text beside them.
I am getting this text from a database and adding to an array in javascript the html formatting and text that I want:
var textHtmlElement = '<label class="smallPadding"><input type="checkbox">' + text + '</label>';
textList.splice(textList.length , 0, textHtmlElement);
The titles array is returned from a function is then assigned a $scope variable:
$scope.list = service.getTextList();
However, when I try to ng-repeat over that array, the checkboxes and text does not show up.
<div class="checkbox" ng-repeat="item in list">
{{ item }}
</div>
There is a scope variable called list when logging scope it to the console: console.log($scope). What I found odd is that when I log console.log($scope.list), it shows an empty array... If I explicitly do $scope.list = ['1', '2', '3'], the ng-repeat works, so I guess the problem is in this line $scope.list = service.getTextList();
How should I fix this?
EDIT The getTextList() function in my service:
getTextList: function(){
return textList;
}
EDIT 2 My service where I get my data and return the list of text:
app.service('service', function() {
var textList = [];
return {
getTextList: function(){
return textList;
},
dataQuery: function() {
var text = "";
.
.
.
query.find({
success: function (results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
text = " " + object.get('text');
textList.splice(textList.length , 0, text);
}
.
.
.
My html:
<div class="checkbox" ng-repeat="item in list">
<label class="smallPadding"><input type="checkbox">{{ item }}</label>
</div>
My controller remains the same - though the array assigned to the $scope.list variable does not contain html. It is just data, as suggested. First, I call the dataQuery function in my service, then the getTextList function.
Edit 3
Working code.
I added the following around my $scope.list = service.getTextList();:
$timeout(function(){
$scope.list = service.getTextList();
}, 150);
The 150 ms timeout gives time for the query to finish getting the data which will be assigned to the $scope.list variable.
service.getTextList();Why don't you post the code ofgetTextList()then? Also, you're doing it plain wrong. HTML should be generated by the view, not by services. Services should return data, not HTML.