Atul Kumar
Atul Kumar

Reputation: 83

Unable to change variable value attached to scope in angular js?

value of $scope.user.name should be changed to 'please enter name' , i should get 'please enter name' when click on reset button.

$scope.user = {};
$scope.reset =function(dta){
       $scope.dta = 'please enter name';
     }

       <input type ="text" ng-model="user.name">

       <button ng-click="reset('user.name')">Reset</button>

Upvotes: 2

Views: 129

Answers (4)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Just remove '' from user.name

Convert this

<button ng-click="reset('user.name')">Reset</button>

to this

<button ng-click="reset(user.name)">Reset</button>

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222722

When you pass parameter dont use ''

<input type ="text" ng-click="reset(user.name)">

DEMO

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.dta ="test";
$scope.reset =function(dta){
       $scope.dta = 'please enter name';
     }
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
     
    <input type ="text" ng-model="dta">
     <button ng-click="reset(user.name)">reset</button>
</div>
</body>
</html>

Upvotes: 0

TheSameSon
TheSameSon

Reputation: 369

You need to pass object and property separately, not as a single string.

And it is much better to use attribute placeholder instead of applying it to the object.

https://jsfiddle.net/wtq0mmgj/1/

UPDATE:

passing user.name without '' won't work because you apply value to $scope.dta instead of target object, so you'll be able to see the value in {{dta}}, but not in {{user.name}}

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32810

Try this:

$scope.reset =function(){
  $scope.user.name= 'please enter name';
}

<input type ="text" ng-model="user.name">

<button ng-click="reset()">Reset</button>

Upvotes: 0

Related Questions