Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am not sure if my Title makes sense, but what I am looking to do is this: I'm a noob to AngularJS and I can't seem to find the answers within the forum, but real basic for some, but for me, I can't seem to figure it out.

I have a variable {{ firstName }} in the HTML that I would like to show on the web page.

Below are the HTML, Demo.js and Data.js code.

Using console.log, I can pull the data., but I can't seem to bind to the variable firstName.

Any thoughts would be greatly appreciated.

Thanks!!!

HTML Code

<!DOCTYPE html>
<html lang="en" ng-app="Demo">
<head>
    <meta http-equiv="x-ua-compatible" content="IE=8">

    <title>AngularJS</title>

    <script type="text/javascript" src="../WebContent/js/angularjs/1.2.20/angular.min.js"></script>

    <script type="text/javascript">
        var $userId = 101;
    </script>

    <script type="text/javascript" src="../Webcontent/js/Demo.js"></script>
    <script type="text/javascript" src="../Webcontent/js/Data.js"></script>
</head>
<body ng-controller="getInfo">
    <b class="header_top_links header_top_links_small welcome_rollover mr5">Welcome {{ firstName }}</b>
</body>
</html>

Demo.js

var app = angular.module("Demo",[]);
app.controller('getInfo', function( $scope ) {

    for (var i = 0; i < users.length; i++) {
        $scope.userId = users[i].id;
        $scope.firstName = users[i].firstName;

        if($userId == $scope.userId) {
            if($scope.userId == 102) {
                console.log("user ID: " + $scope.userId);
            } else if ($scope.userId == 101) {
                console.log("user ID: " + $scope.userId);
                console.log("user first name: " + $scope.firstName);
            } else {
                console.log("user ID: " + $scope.userId);
            }
        }
    }
});

Data.js

var users = [
     {
        id: 100,
        firstName: "Guest"
     }, {
        id: 101,
        firstName: "Basick"
     }, {
         id: 102,
         firstName: "Advanced"
     }
 ];
share|improve this question
    
Aside from your loop probably not producing the result you wanted, this code is pretty much correct. Working plunk. Is one of your scripts not in the right place or something? – Dark Falcon Sep 2 '14 at 17:56
    
Thx, @DarkFalcon for the quick response. My page was caching reason why the variable wasn't showing. It's working now. – crea8ive_mind Sep 2 '14 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.