$watch'ing Angular 指令中的数据变化 [英] $watch'ing for data changes in an Angular directive

查看:26
本文介绍了$watch'ing Angular 指令中的数据变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Angular 指令中操作数据时触发 $watch 变量(例如,插入或删除数据),但不为该变量分配新对象?

How can I trigger a $watch variable in an Angular directive when manipulating the data inside (e.g., inserting or removing data), but not assign a new object to that variable?

我有一个当前正在从 JSON 文件加载的简单数据集.我的 Angular 控制器执行此操作,并定义了一些函数:

I have a simple dataset currently being loaded from a JSON file. My Angular controller does this, as well as define a few functions:

App.controller('AppCtrl', function AppCtrl($scope, JsonService) {
    // load the initial data model
    if (!$scope.data) {
        JsonService.getData(function(data) {
            $scope.data = data;
            $scope.records = data.children.length;
        });
    } else {
        console.log("I have data already... " + $scope.data);
    }

    // adds a resource to the 'data' object
    $scope.add = function() {
        $scope.data.children.push({ "name": "!Insert This!" });
    };

    // removes the resource from the 'data' object
    $scope.remove = function(resource) {
        console.log("I'm going to remove this!");
        console.log(resource);
    };

    $scope.highlight = function() {

    };
});

我有一个

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆