为什么更改姓氏时模型中的全名不会更新

Why does full name not update in model when changing the last name?

本文关键字:全名 更新 模型 为什么      更新时间:2023-09-26

你能告诉我为什么在更改姓氏时模型中的全名没有更新吗?

普伦克http://plnkr.co/edit/cqmwJc5dpoDWvai4xeNX?p=preview

var loginCntrl=function($scope){
  $scope.testClick =function(){
    $scope.lastname="kumar";
     console.log($scope.fullname)
      alert($scope.fullname)
  }
$scope.name="naveen";
$scope.lastname="sharam";
$scope.fullname=$scope.name+$scope.lastname;
}

它在单击按钮时显示"naveensharma"。但是在按钮点击时,我改变了我的姓氏。它应该在模型中显示"naveenkumar"。为什么不显示?换句话说,我更改了姓氏变量"kumar",预期结果是"naveenkumar"

您需要

fullname 使用计算属性,否则该属性不会订阅对firstnamelastname的任何更改

$scope.fullname = function() {
    return $scope.firstname + $scope.lastname;
};