如何在点击按钮时更新变量?

时间:2015-01-12 10:16:55

标签: angularjs angularjs-directive angularjs-scope

你能不能告诉我如何在点击按钮时更新变量?实际上我有三个变量first,lastname和fullname .on按钮点击我更改姓氏但它不反映在模型上?我可以使用摘要或观看功能吗?

var loginCntrl=function($scope){
  $scope.testClick =function(){
    alert('sss');
    $scope.lastname="kumar";
     $scope.watch(function($scope){
       return $scope.name;
     }, function($scope){
       return $scope.name;
     })

  }

$scope.name="naveen";
$scope.lastname="sharam";
$scope.fullname=$scope.name+$scope.lastname;
}

3 个答案:

答案 0 :(得分:1)

<button ng-click="lastName='jonathan'">Test</button> 

如果您只想更改一个参数,可以在点击时更改变量,例如 ng-click =&#34; lastName =&#39; jonathan&#39; ,如果您愿意操纵多个变量,然后调用如下函数,

函数调用:

<强>纳克单击=&#34; changeValues()&#34;

$scope.changeValues=function(){
  $scope.lastName="Jonathan";
  //Other code comes here
}

答案 1 :(得分:0)

将您点击处理程序更改为以下

$scope.testClick =function(){
        alert('sss');
        $scope.lastname="kumar";
        $scope.fullname=$scope.name+$scope.lastname;
      }

答案 2 :(得分:0)

嘿,发现jsfiddle很简单你不需要使用$ watch只需更新$scope.lastname中的姓氏:

"http://jsfiddle.net/c9fru8ky/"