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.
jsfiddle link here
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>
Comments
Post a Comment