当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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

网友答案:

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>
网友答案:

Try this:

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

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

<button ng-click="reset()">Reset</button>
网友答案:

This is because you are passing value with '' So it will be converting as string

Try calling ng-click="reset(user.name)" without ''

<button ng-click="reset(user.name)">Reset</button>
网友答案:

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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: