angular $ watch和从数据库中检索数据的问题

bor*_*ytm 2 model-view-controller controller angularjs

我是一个新手编程试图将一个Web应用程序与Angular,node.js和图形数据库neo4j组合在一起.

我想根据用户选择(或拒绝)术语(点击按钮)动态加载数据库中的内容.每次单击按钮时,相关术语都会添加到数组中(排除或包含).这个想法是每次选择新术语时都会对数据库进行新的调用.

我现在停留在如何调用数据库来检索内容.我正在尝试使用$ watch观察阵列的变化.出现问题,我遇到了解决问题的麻烦.

这是控制器代码:

angular.module('myApp.controllers', []).    
controller('content',function($scope,$http, queryTerms, $watch){


    //watch arrays of terms for changes and fetch results based on what is selected
    $watch(function() { return angular.toJson( [ queryTerms.includedTerms, queryTerms.excludedTerms ] ) },
        function() {
            $http({
                method:'get',
                url:'/query/getContent',   
                params: {includeTerms:queryTerms.includedTerms , excludeTerms:queryTerms.excludedTerms}

            }).
            success(function(data){
                    //feed content data to display for viewing

            }).
            error(function(data){
                    $scope.test = "Error :("

            });
        });

});
Run Code Online (Sandbox Code Playgroud)

当我使用$ watch时,我收到以下错误:

Error: Unknown provider: $watchProvider <- $watch
Run Code Online (Sandbox Code Playgroud)

这是一种可怕的愚蠢方式来解决这个问题吗?任何建议都将非常感激 - 我正在学习,因为我到目前为止,我在这里得到的建议是惊人的.谢谢!

zs2*_*020 6

$scope.$watch改用.

controller('content', function ($scope, $http, queryTerms) {
    $scope.$watch(function () {
        return angular.toJson([queryTerms.includedTerms, queryTerms.excludedTerms])
    },...
Run Code Online (Sandbox Code Playgroud)