小编Har*_*han的帖子

将数组转换为链表 - 来自 Eloquent Javascript

这是我无法理解的书中的挑战之一,或者我的大脑无法分解它。这是求解函数:

 function arrayToList(array) {
  var list = null;
  for (var i = array.length - 1; i >= 0; i--)
    list = {value: array[i], rest: list};
  return list;
}

console.log(arrayToList([10, 20]));
// ? {value: 10, rest: {value: 20, rest: null}}
Run Code Online (Sandbox Code Playgroud)

所以我们反向循环数组,所以第一次列表应该是:

list = {value:20, rest:{value:20, rest:**mind blows here**}}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我完成这个过程吗?

javascript arrays linked-list

3
推荐指数
2
解决办法
5588
查看次数

试图在2个控制器之间共享动态变量 - Angular

变量已命名searchText,它将由用户在搜索框中键入

<input type="text" class="form-control" ng-model="searchText"  placeholder=" Type KvK-nummer and Press Enter" id="typehead">
Run Code Online (Sandbox Code Playgroud)

示例网址: http://localhost:8091/odata/dll-poc-dv/Account('41-125061-0000') //in brackets is the searchTeaxt

我想要实现的是保持searchText的值并在2个不同的控制器中使用以传递URL中的searchText来接收数据.所以我得到了服务中的变量并与2个控制器共享该服务:

 angular.module('serviceModule',[])
        .factory('dataService',['$http', dataService]);

    function dataService($http,$rootScope,$scope){
      return{
            getSearchText:getSearchText,
            setSearchText: setSearchText,
            getMainData: getMainData
        };
     var searchText;

   function setSearchText(value){
         searchText = value;
      };
   function getSearchText(){
        return searchText;
    };

   function getMainData(id){
       return $http.get("http://localhost:8091/odata/dll-poc-dv/Account(kvk='"+id+"')").then(
            function (result){ console.debug(result);return result.data.d.results})

    };
$scope.$watch('searchText',function(newVal,oldVal){
            console.log(newVal,oldVal);
        })
};
Run Code Online (Sandbox Code Playgroud)

第一控制员:

    angular.module('mainPage',['serviceModule'])
      .controller('MainCtrl',['$scope', '$http','dataService', function ($scope, $http,dataService) {

   dataService.setSearchText($scope.searchText);

    $scope.getMainData = function(kvk){ 
    $scope.getMainData = function(){
        dataService.getMainData($scope.searchText).then(function(data){
            $scope.getData= data;
        })
        }; …
Run Code Online (Sandbox Code Playgroud)

service controller angularjs

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