curran / screencasts

Code that goes along with my screencasts.
https://www.youtube.com/playlist?list=PL9yYRbwpkyktAZaphR2UfeYpgNGnIqjs9
MIT License
2.62k stars 2.03k forks source link

Initializing model using a Global function doesn't seem to work for me? #14

Open darshakmehta opened 8 years ago

darshakmehta commented 8 years ago

What is going wrong here ?

<!doctype html>
<html lang="en" data-ng-app>
<head>
    <meta charset="utf-8">
    <title>AngulasJS Demo3</title>
    <script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script>
        function nameController($scope){
            $scope.firstname = 'John';
            $scope.lastname = 'Smith';
        }
    </script>
</head>
<body data-ng-controller = "nameController">
First Name : <input type = "text" data-ng-model="firstname"></input> 
<br />
<br />
Last Name : <input type = "text" data-ng-model="lastname"></input> 
<br />
<br />
Hello {{ firstname }} {{lastname}}

</body>
</html>

demo

BrahamMounir commented 8 years ago

Hi, me to I got the same problem it seems that it doesn't work fine ! do we miss something ?

jainvishal520 commented 7 years ago

@tech-boy You need to initialize angular app and controller like this


<!doctype html>
<html lang="en" data-ng-app='myapp' >
<head>
    <meta charset="utf-8">
    <title>AngulasJS Demo3</title>
    <script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script>
        angular.module('myapp',[])
          .controller('nameController',nameController);

        function nameController($scope){
            $scope.firstname = 'John';
            $scope.lastname = 'Smith';
        }
    </script>
</head>
<body data-ng-controller = "nameController">
First Name : <input type = "text" data-ng-model="firstname"></input> 
<br />
<br />
Last Name : <input type = "text" data-ng-model="lastname"></input> 
<br />
<br />
Hello {{ firstname }} {{lastname}}

</body>
</html>