javascript - Angular 模型未针对对象的内部属性进行更新

标签 javascript angularjs

下面有一个模型对象“o”,它引用另一个作用域属性 testText。我希望当 $scope.testText 更新时,$scope.o.filed1 将自动更新。

var app = angular.module('app', ['kendo.directives']); 
app.controller("myCtrl", function ($compile, $scope) {
$scope.o={};
$scope.testText = 'hello';
$scope.o.filed1 = $scope.testText;

但是它并没有按照我的预期工作。这是我的在线示例。

http://plnkr.co/edit/ZHS8EdcCb5EJwQ8UUenj?p=preview

最佳答案

这不会起作用,因为它只是一个作业。试试这个:

var app = angular.module('app', ['kendo.directives']); 

app.controller("myCtrl", function ($compile, $scope) {
   $scope.o={};
   $scope.testText = 'hello';
   $scope.$watch('testText', function(newValue){
      $scope.o.filed1 = newValue;
   });
});

关于javascript - Angular 模型未针对对象的内部属性进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926743/

相关文章:

javascript - 使用 "a href"启动 javascript 函数

javascript - 正则表达式分组步骤说明

javascript - 单击处理程序事件监听器

javascript - img与js交换迭代超时

angularjs - 剑道网格/Angular : cannot create grid columns/data dynamically

javascript - 将动态数据传递给 angularjs 指令的正确方法

javascript - $http - header 未返回 - AngularJS

javascript - 使用 JQuery 禁用下拉菜单

html - 鼠标悬停在 d3 圆环图上时图例文本背景发生变化

javascript - 无法显示 Angular 形式的跨度