小编JEu*_*vin的帖子

76
推荐指数
5
解决办法
5万
查看次数

如何使用JavaScript/Prototype 1.7递归搜索对象树并根据键/值返回匹配对象

我有一些嵌套的对象数据,我想搜索它并根据id返回匹配的对象.

var data = [{id: 0, name: 'Template 0', subComponents:[
        {id: 1, name: 'Template 1', subItems:[
            {id: 2, name: 'Template 2', subComponents:[{id: 3, name: 'Template 3'}], subItems: [{id: 4, name: 'Template 4'}]}
        ]}
    ]}
];
Run Code Online (Sandbox Code Playgroud)

所以我想做这样的事情

getObjectByKeyValue({id: 3}) 
Run Code Online (Sandbox Code Playgroud)

并让它回来

{id: 3, name: 'Template 3'}
Run Code Online (Sandbox Code Playgroud)

它有点必须完成,因为我有subItems,AND子组件,每个子组件都可以有孩子.

我尝试使用Prototype 1.7并没有运气 - 我认为这只是搜索一个数组,而不是一个带有它的子节点的树:

data.find(function(s){return s.id == 4;})
Run Code Online (Sandbox Code Playgroud)

提前致谢!!!!!!

javascript tree recursion prototype javascript-objects

16
推荐指数
2
解决办法
2万
查看次数

如何在ng-grid中使用ExternalSorting,在sortInfo上使用$ watch?

我的代码中的ng-grid有以下设置:

$scope.gridOptions = {
    ...
    useExternalSorting : false,
}

$scope.watch('gridOptions.ngGrid.config.sortInfo', function (oldValue, newValue) {
    console.log(newValue)
})
Run Code Online (Sandbox Code Playgroud)

我也试过用sortInfo : undefined$watch(gridOptions.sortInfo).这似乎仅在网格最初加载时起作用.之后,当我点击标题列时,它似乎没有进入回调函数$watch.我尝试在回调函数中放置一个调试器来触发排序,我可以看到代码用正确的信息更新sortInfo数组,但是它似乎没有进入watch语句的回调函数.我的设置有什么不对吗?我在这里有一个类似于我正在尝试的东西.

angularjs angularjs-scope ng-grid angular-ui-grid

5
推荐指数
1
解决办法
5558
查看次数

Angularjs错误未知提供者:$ scopeProvider < - $ scope < - user

我收到了错误

未知提供商:$ userProvider < - $ user"

使用以下代码:

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

app.factory("user", function($scope, $http) {
   var usr = {};
   usr.getAllLists = function(){
       return "test";
   }
   return usr;  
});

cart.controller("MyController", ["$scope", "$http", "$user", 
    function ($scope, $http, user){

        $scope.initialize = function(){
            $scope.lists = user.getAllLists();
        }

    }
]);
Run Code Online (Sandbox Code Playgroud)

你看错了吗?

javascript angularjs angularjs-factory angular-controller

3
推荐指数
1
解决办法
3628
查看次数