当在模板中使用方法时,Angular中的双向数据绑定

Two way databinding in Angular when using method in template

本文关键字:Angular 数据绑定 使用方法      更新时间:2023-09-26

有没有办法告诉angular $scope中的特定函数依赖于$scope中的一些其他字段?我想在数据模型中隐藏一些功能,例如:

$scope.user = {
   name: "John",
   surname: "Smith",
   getUsername: function() {
      return [this.name, this.surname].join(' ');
   }
}

然后我想在html模板中使用$scope.user.getUsername(),但当我更改$scope.user.name时,模板不会刷新。有没有一种方法可以告诉angular这个函数依赖于其他字段?

好的,原来Angular是自动完成的,但我在不使用$apply的情况下更改了它。