AngularJS属性更改事件/列表上的侦听器<;模型>;

AngularJS属性更改事件/列表上的侦听器<;模型>;,angularjs,Angularjs,在那里。我需要一个监视程序或事件,当模型(详细信息)上的属性(在列表(产品)中保留)发生更改时,该事件将被触发 视图: 这个问题有angularjs解决方案吗?问候 @Shomz我用下面的代码试了一下手表。但当属性c.expires在输入字段中更改时,手表将不会升起 $scope.$watch("c.products.expire", function (newValue, oldValue) { log.debug(newValue); }); 编辑:更具体地

在那里。我需要一个监视程序或事件,当模型(详细信息)上的属性(在列表(产品)中保留)发生更改时,该事件将被触发

视图:

这个问题有angularjs解决方案吗?问候

@Shomz我用下面的代码试了一下手表。但当属性c.expires在输入字段中更改时,手表将不会升起

    $scope.$watch("c.products.expire", function (newValue, oldValue) {
        log.debug(newValue);
    });
编辑:更具体地说:
产品列表将在运行时生成。用户还可以添加和删除项目。还应监视此“临时”添加的对象。

您可以在$scope.products数组变量中的每个项目上设置$watch,并以这种方式执行事件处理

_.each(products, function(product) {
    $scope.$watch(function() { return product; }, function(newVal, oldVal) {
        if(newVal.name != oldVal.name || newVal.expire != oldVal.expire || ...) {
            //do event work
            product._isDirty = true;
        }
    });
});

或者,如果您将其包装在一个表单中,您可以检查该表单的$dirty变量,看看是否有。

谢谢您将我带到了正确的方向@wbeange。这是我目前的解决方案。$watchlist没有像我预期的那样工作,所以我使用了“旧”的$watch

$scope.$watch(“c.products”,
函数(newValue、oldValue){
//列表的首次初始化
if(typeof newValue!=“未定义”&&c.products.length>0){
if(newValue!=oldValue&&oldValue!=null){
对于(var i=0;i
您是否尝试过更改?Angular在表单上有一个“脏”属性。。。请参见:设置c.dirty=true这样的属性;对属性进行更改只是一个例子。当属性发生更改时,我需要运行自定义代码。谢谢,我将尝试此操作。当我添加一个项目-一个产品时,我必须将它也添加到$scope中。$watch?@Briefkasten是的,但要小心。我可能误解了这种情况。如果您一直在管理该阵列,就不会一遍又一遍地声明手表。更具体地说,您想做什么?@Briefkasten作为双向绑定的一部分,如果您只是将新的对象数组重新分配给$scope.products变量,视图中的列表将自动更新。如果这就是你想要的,你就不需要手表了。我需要在特定属性改变时执行一些webapi调用。
    $scope.$watch("c.products.expire", function (newValue, oldValue) {
        log.debug(newValue);
    });
_.each(products, function(product) {
    $scope.$watch(function() { return product; }, function(newVal, oldVal) {
        if(newVal.name != oldVal.name || newVal.expire != oldVal.expire || ...) {
            //do event work
            product._isDirty = true;
        }
    });
});
$scope.$watch("c.products",
               function (newValue, oldValue) {
                   //first initialising of list
                   if (typeof newValue != 'undefined' && c.products.length > 0) {

                       if (newValue != oldValue && oldValue!=null) {

                           for (var i = 0; i < newValue.length; i++) {
                               if (newValue[i].expire!= oldValue[i].expire
                                   || newValue[i].otherprop != oldValue[i].otherprop)
                               {
                                   //todo
                               }
                           }

                       }
                   }
               },
               true // Object equality (not just reference).
           );