Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
Javascript 在重新创建数组以停止重复之前,请清除数组。在数据刷新或重新加载时,请在HTML中选择下拉选项_Javascript_Angularjs_Arrays - Fatal编程技术网

Javascript 在重新创建数组以停止重复之前,请清除数组。在数据刷新或重新加载时,请在HTML中选择下拉选项

Javascript 在重新创建数组以停止重复之前,请清除数组。在数据刷新或重新加载时,请在HTML中选择下拉选项,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,当我刷新数据时(基本上是在重新调用创建数组的方法时),我的角度数组项会重复,因为它会将相同的项添加到预先存在的数组中 当我通过在我的中选择一个下拉帐户ID在帐户之间切换时会发生这种情况 这是我更改帐户/个人资料的HTML代码: <div ng-show="showSelectTwitterAccounts"> <div> <div id="DropDownBox"> <label for="ViewTwit

当我刷新数据时(基本上是在重新调用创建数组的方法时),我的角度数组项会重复,因为它会将相同的项添加到预先存在的数组中

当我通过在我的
中选择一个下拉帐户ID在帐户之间切换时会发生这种情况

这是我更改帐户/个人资料的HTML代码:

<div ng-show="showSelectTwitterAccounts">
    <div>
        <div id="DropDownBox">
            <label for="ViewTwitterProfile">Select Twitter Profile: </label>
            <select ng-change="changeTwitterProfile()" ng-model="selectedTwitterProfileID" id="ViewTwitterProfile" ng-options="channelID for channelID in twitterChannels"></select>
        </div>
    </div>
</div>
function getChannelProfiles() {

    $scope.showSelectTwitterAccounts = true;

    GetUserAccessService.returnBrandProfileID().then(function (brandProfileID) {

        SocialMediaUserService.channelProfiles().then(function (channelProfiles) {

            channelProfiles.forEach(function (channel) {

                if (channel.brand_profile_id === brandProfileID && channel.channel_type === 'twitter') {

                    $scope.twitterChannels.push(channel.id);
                }
            });
            console.log($scope.twitterChannels);
        });
    });
}
$scope.changeTwitterProfile = function () {

    $scope.dataModel = DataModelService.showDataLoadingAnimation();

    $scope.twitterPageMetrics = {};

    $scope.dataContent = false;

    $scope.showSelectTwitterAccounts = true;

    var twitterID = $scope.selectedTwitterProfileID;

    GetFusionDataService.getItems(getRequestURL(twitterID))

      .success(function (data) {
          handleDataRetrievalSuccess(data);
          getChannelProfiles();

      })

      .error(function (error, status) {
          handleDataRetrievalError(error, status);
      });

};
这是我切换帐户时调用的方法:

<div ng-show="showSelectTwitterAccounts">
    <div>
        <div id="DropDownBox">
            <label for="ViewTwitterProfile">Select Twitter Profile: </label>
            <select ng-change="changeTwitterProfile()" ng-model="selectedTwitterProfileID" id="ViewTwitterProfile" ng-options="channelID for channelID in twitterChannels"></select>
        </div>
    </div>
</div>
function getChannelProfiles() {

    $scope.showSelectTwitterAccounts = true;

    GetUserAccessService.returnBrandProfileID().then(function (brandProfileID) {

        SocialMediaUserService.channelProfiles().then(function (channelProfiles) {

            channelProfiles.forEach(function (channel) {

                if (channel.brand_profile_id === brandProfileID && channel.channel_type === 'twitter') {

                    $scope.twitterChannels.push(channel.id);
                }
            });
            console.log($scope.twitterChannels);
        });
    });
}
$scope.changeTwitterProfile = function () {

    $scope.dataModel = DataModelService.showDataLoadingAnimation();

    $scope.twitterPageMetrics = {};

    $scope.dataContent = false;

    $scope.showSelectTwitterAccounts = true;

    var twitterID = $scope.selectedTwitterProfileID;

    GetFusionDataService.getItems(getRequestURL(twitterID))

      .success(function (data) {
          handleDataRetrievalSuccess(data);
          getChannelProfiles();

      })

      .error(function (error, status) {
          handleDataRetrievalError(error, status);
      });

};
我理解为什么会发生重复。阵列未被清除,我一直将相同的ID推入阵列。

我尝试了几种方法来阻止这种情况,比如添加

$scope.twitterChannels = []; 
在乞求我创建视图的方法时。这将按预期将阵列设置为空阵列,并且重新创建阵列时不会重复

然而,当我调用我的
$scope.changeTwitterProfile
方法时,一切都按计划进行,直到再次创建阵列为止。数组被清除,并进行数据调用以检索要为数组筛选的数据,但在返回数据之前,系统进入my
$scope.changeTwitterProfile
方法,此时数组仍然为空,并且找不到配置文件。在此之后,数组将按其应该的方式创建

为冗长的解释道歉-我只是想弄清楚我做了什么以及发生了什么

谢谢你的帮助

根据我的理解, 您正在清除表示选择框选项的ID数组

以下是您采取的步骤

  • 清除进行了$http调用的数组
  • 有两个$http服务调用
  • 用新元素填充数组
  • 您暂时看不到下拉列表的原因是,在$http承诺实现之后,angular运行摘要周期。在您的情况下,当您的第一个承诺GetUserAccessService.returnBrandProfileID()已履行,而您的第二个承诺SocialMediaUserService.channelProfiles()尚未履行时,它将运行

    有两种方法可以解决这个问题

  • 不要在$scope.twitterChannels.push(channel.id)中推送相同的元素
  • 第二次承诺时清除$scope.twitter频道 SocialMediaUserService.channelProfiles()是[在其解析函数中]实现的
  • 据我了解,, 您正在清除表示选择框选项的ID数组

    以下是您采取的步骤

  • 清除进行了$http调用的数组
  • 有两个$http服务调用
  • 用新元素填充数组
  • 您暂时看不到下拉列表的原因是,在$http承诺实现之后,angular运行摘要周期。在您的情况下,当您的第一个承诺GetUserAccessService.returnBrandProfileID()已履行,而您的第二个承诺SocialMediaUserService.channelProfiles()尚未履行时,它将运行

    有两种方法可以解决这个问题

  • 不要在$scope.twitterChannels.push(channel.id)中推送相同的元素
  • 第二次承诺时清除$scope.twitter频道 SocialMediaUserService.channelProfiles()是[在其解析函数中]实现的

  • “当我调用$scope.changeTwitterProfile方法时,一切都按计划进行,直到再次创建数组为止。数组被清除,数据调用用于检索数据以筛选数组,但在返回数据之前,系统会进入我的$scope.changeTwitterProfile方法,此时数组仍然可用。”空,并且找不到配置文件。在此之后,数组将按其应该的方式创建。“您能详细说明一下吗?“找不到个人资料”是什么意思?“当我调用$scope.changeTwitterProfile方法时,一切都按计划进行,直到再次创建阵列为止。数组被清除,并进行数据调用以检索要为数组筛选的数据,但在返回数据之前,系统会进入我的$scope.changeTwitterProfile方法,此时数组仍然为空,并且找不到配置文件。在这之后,数组将按它应该的方式创建。“你能详细说明一下吗?你说的“找不到配置文件”是什么意思“?谢谢:)。当我昨天走出办公室时。它真的像一吨砖头一样击中了我。未解决的承诺。。。。我现在只是在解析后清除数组。工作正常。谢谢:)。当我昨天走出办公室时。它真的像一吨砖头一样击中了我。未解决的承诺。。。。我现在只是在解析后清除数组。一切正常。