Why $scope doesn't change in the alert box? (angularJS) -
i'am trying understand behaviour: when change input view change when click on button alert box don't change $scope.firstname. give me advices. regards
var app = angular.module('myapp', []); app.controller('myctrl', function($scope) { $scope.firstname = "john"; $scope.lastname = "doe"; var test=$scope.firstname; $scope.test=function(){ alert(test); }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp" ng-controller="myctrl"> first name: <input type="text" ng-model="firstname"><br> last name: <input type="text" ng-model="lastname"><br> full name: {{firstname + " " + lastname}} <button ng-click="test()">click</button> </div>
currently, when launch controller, doing 3 things :
$scope.firstname = "john"; $scope.lastname = "doe"; var test=$scope.firstname;
so var test initialize when launch controller not when laucnh function. funtion, print value.
if want print current version of scope, need :
var app = angular.module('myapp', []); app.controller('myctrl', function($scope) { $scope.firstname = "john"; $scope.lastname = "doe"; $scope.test=function(){ alert($scope.firstname); }; });
Comments
Post a Comment