why foreach does not work

28 views
Skip to first unread message

ömer iyiöz

unread,
Jun 15, 2016, 2:51:06 AM6/15/16
to AngularJS
Hello i have two similar code. One is working, the other one does not work.
Below is working code :
<!DOCTYPE html>
<head>
    <title>Welcome in the AngularJS</title>
</head>
<body ng-app="app">
    <div ng-controller="forEachController">
        <p ng-repeat="pr in prop">Properties: {{pr}}</p>
        <p ng-repeat="vl in val">Value: {{vl}}</p>
        {{obj}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('forEachController', ['$scope', function ($scope) {
        $scope.prop = [];
        $scope.val = [];
        $scope.obj = [];
        angular.forEach([{ name: 'Todd', location: 'UK' },{ name: 'Todd', location: 'UK' }], function (value, prop, obj) {
            $scope.val.push(value);
            $scope.prop.push(prop);
            $scope.obj.push(obj);
        });
    }]);

</script>




Below does not work. I couldn't solve it. How could i fix that and get a similar result with the above working code?
<!DOCTYPE html>  
<html ng-app="" xmlns="http://www.w3.org/1999/xhtml">    
<head>  <!-- www.techstrikers.com -->  
<title>My first AngularJS code</title>    
</head>    
<body bgcolor="#bnde45">  
 
 
<script>    
angular.forEach([{ name: 'Todd', location: 'UK' },{ name: 'Todd', location: 'UK' }], function (value, prop, obj) {
document.write(value);
document.write(prop);
document.write(obj);
});
</script>  
</body>    
</html>


ömer iyiöz

unread,
Jun 15, 2016, 2:52:57 AM6/15/16
to AngularJS
I'm wondering why that does not work. Because everything looks like fine..

Sander Elias

unread,
Jun 15, 2016, 5:16:06 AM6/15/16
to AngularJS
Hi Ömer,

you can't use document.write in this fashion. It will probably just fail silently (seem to do nothing)

Regards
Sander

ömer iyiöz

unread,
Jun 15, 2016, 5:19:33 AM6/15/16
to ang...@googlegroups.com
OK, one question more : what is obj? Does it refer to the first param of angular.foreach() method?

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/2e7Pj9oTow4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
İyi çalışmalar
Ömer İyiöz
0546 828 62 33

Sander Elias

unread,
Jun 15, 2016, 5:31:34 AM6/15/16
to AngularJS
hi Ömer,

Well, as you can clearly read in the first paragraph of the forEach documentation, it is indeed!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages