angularjs - Initializing the Angular Model using an Angular Controller -


<!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> 

initialized model global function have declare scope object , using passing firstname , lastname view.

output :

here firstname , lastname values not getting reflected.

enter image description here

jsfiddle link here

edit : found code here, , output.

i reference app directly declaring variable in javascript, , referencing name of app in declaration in html tag. following work:

<html lang="en" data-ng-app="myapp"> <head>     <title>angulasjs demo3</title>     <script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>     <script>         var myapp = angular.module('myapp', []);         myapp.controller('namecontroller', ['$scope', function($scope){             $scope.firstname = 'john';             $scope.lastname = 'smith';         }]);     </script> </head> <body data-ng-controller="namecontroller">     first name : <input type = "text" data-ng-model="firstname"></input>      last name : <input type = "text" data-ng-model="lastname"></input>      hello {{ firstname }} {{lastname}} </body> </html> 

jsfiddle


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -