Answer the question
In order to leave comments, you need to log in
Why doesn't Angular work with a loop?
When writing a loop in js, all Angular script and attributes are turned off, and only the loop itself is displayed.
How can I fix it?
Whole Script:
var myApp = angular.module("myApp" , []);
myApp.controller("firstCtrl", function($scope){
$scope.name = "World";
});
myApp.controller('PhoneListCtrl', function($scope){
$scope.phones = [
{'name' : 'Nexus',
'snippet' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
'status': true},
{'name' : 'HTC',
'snippet' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
'status' : false},
{'name' : 'Huawei',
'snippet' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
'status' : true}
];
var date = new Date();
$scope.today = date;
//Цикл
myNumber = 10;
function newFunc(number){
newArray = new Array(number);
for (var i = 0; i < newArray.length; i++) {
newArray[i] = 'Number: ' + (i + 1) + '<br />';
document.write(newArray[i]);
};
};
newFunc(myNumber);
});
Answer the question
In order to leave comments, you need to log in
As far as I understood from your description of the problem, the point is document.write - it simply overwrites everything that is on the page ( more ).
You need to write in the innerHtml of some specific element - like this :
var myNumber = 10;
function newFunc(number) {
var el = document.getElementById('bar'),
content = '';
for (var i = 1; i <= number; i++) {
content += 'Number: ' + i + '<br />';
}
el.innerHTML = content;
}
newFunc(myNumber);
var myNumber = 10;
function newFunc(number) {
var newArray = [],
str;
for (var i = 0; i < number; i++) {
str = 'Number: ' + (i + 1) + '<br />';
newArray.push(str);
console.log(str);
}
}
newFunc(myNumber);
var myNumber = 10;
function newFunc(number) {
for (var i = 1; i <= number; i++) {
console.log('Number: ' + i + '<br />');
}
}
newFunc(myNumber);
Here I look and wonder why you need an argular?
document.write is the cause of your problem. Just give it up. All this satan that you do in the controller can be done in the template (and you need to do it there).
<ul>
<li ng-repeat="phone in phones">Number: {{$index + 1}}</li>
</ul>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question