Desired result is when user input data into the input field, it will dynamically display user’s name below. eg. When a user name Sally input her name in the input field, in the below it will instantly show her name.
Any idea?Thanks
var app = angular.module('app', []);
app.controller('PersonCtrl', function(){
//...
this.name ='tom';
this.test ='mary';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<html ng-app="app">
<head>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css">
</head>
<body>
<div class="well span6" ng-controller="PersonCtrl as tim">
<label>Name1:</label>
<input type="text" ng-model="name">
<hr>
<label>Name2:</label>
<input type="text" ng-model="test">
<br>Name1: {{tim.name }}<br>
Name2: {{tim.test }}
</div>
</body>
</html>
3
Answers
If you want to use
this
andng-cpntroller as
you should always use yourtim
in the templateJust this will be OK,:
You may try for this:
If you want to stick with the
Controller as
syntax, you can use it like this:Notice that
name
andtest
are properties of the controller, so you have to bind them as such through theng-model
directive.Your original syntax would’ve been correct if
name
andtest
were properties of the$scope
object.