Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJs forEach logic,不那么学术,但很实用_Angularjs_Foreach_Angular Promise - Fatal编程技术网

AngularJs forEach logic,不那么学术,但很实用

AngularJs forEach logic,不那么学术,但很实用,angularjs,foreach,angular-promise,Angularjs,Foreach,Angular Promise,我已经有了这个代码,但我认为这不是正确的方法。我想我需要使用$q,但这对我来说很难理解 我从我的angularJs工厂,从我的php服务器mysql后端正确地接收到这个漂亮的json数组(data.data)。这是一些带有开始日期的rendez VOU: [{"id":"1","title":"Loi Travail","infos":null, "adresse":"12 avenue des lis 78013 paris", "criticite":"4","f

我已经有了这个代码,但我认为这不是正确的方法。我想我需要使用$q,但这对我来说很难理解

我从我的angularJs工厂,从我的php服务器mysql后端正确地接收到这个漂亮的json数组(data.data)。这是一些带有开始日期的rendez VOU:

   [{"id":"1","title":"Loi Travail","infos":null,
     "adresse":"12 avenue des lis 78013  paris",
     "criticite":"4","fichiers":null,
     "start":"2017-06-11T22:37:59.012Z"},
    {"id":"17","title":"jjtyjyjt","infos":"jytjjyjyj",
     "adresse":"tjtyjjyj","criticite":"7","fichiers":"",
     "start":"2017-06-11T22:37:59.012Z"}]
问题是angular material datetimepicker无法识别开始日期,因为它是一个字符串,所以我需要执行一个循环来添加新的日期(),以转换每个“开始”元素

所以,我已经完成了这段简短的代码,,它正在发挥作用

    rdvFactory.get_rdvs().then(function(data){ 

        $scope.events = data.data;

        angular.forEach($scope.events,function(value,index){

            value.start = new Date(value.start);

        })
   })
它正在工作,这意味着我可以看到angular material datetimepicker中出现的所有rendez VOU,但我认为这不是正确的方法,因为我在angular.forEach的每次迭代中都会多次更改作用域(双向绑定),我想这非常耗时

我想我应该在data.data上做一个foreach来转换日期,在转换完成之后,我应该只在这之后设置$scope.events

我不知道你是否明白我的意思,但是使用$q是很困难的,如果有人能多给我解释一下$q,我将不胜感激,因为即使使用stackoverflow的例子,我也很难理解语法

问:你认为增加一秒更好吗?然后像这样

rdvFactory.get_rdvs().then(function(data){ 
    $scope.events = data.data;
    }).then(function(data){
        angular.forEach($scope.events,function(value,index){
            value.start = new Date(value.start);
        })
});
在最后一个示例中,您认为第二个.then是否真的要等待$scope.events被填充?我不确定。。。这是可行的,但这是避免性能问题的正确方法吗


祝你有愉快的一天

尼克-你说得对,更改$scope变量代价高昂,并且会产生不必要的摘要周期。我建议你这样做-

rdvFactory.get_rdvs().then(function(data){
    var tempData = data.data;  // A temp local variable

    angular.forEach(tempData ,function(value,index){
        value.start = new Date(value.start);
    }

    $scope.events = tempData; // assign the final JSON to scope variable.
})
对于$q,它主要用于组合两个或多个AJAX响应,然后执行操作。例如—

如果有一个AJAX调用来获取用户详细信息(基于ID)和另一个调用来获取用户访问权限(基于ID),那么我们可能希望同时调用它们,因为这两个调用都需要用户成功登录

错误流量-

Fetch User details.on success then( 
   //fetch user access right // on success of user details call fire user access rights.
)
相反,您将两个调用都排在一个数组中,等待它们一起通过或失败

$q = [call1, call2]
$q.then ( 
    //on success, we know that user is correct and has right user access. 
)

非常感谢,我将回顾这篇文章并尝试,祝你有愉快的一天