gpt4 book ai didi

javascript - 如何在此处使用 $watch 以便在指令 USING ui-router 之前触发 API

转载 作者:行者123 更新时间:2023-12-03 02:15:21 26 4
gpt4 key购买 nike

编辑->我也在使用 ui-router-我可以使用解析吗 名称:'计划.摘要',

  state: {
url: "/summary",
templateUrl: '../script/planSummary.html',
controller: "summaryCtrl",params:{obj:null}
}
}
]

我试图在 Controller 中的指令之前触发 API。该指令需要调用我的 API 来获取数据,以便它可以填充在页面上。

当我加载页面时,指令会触发,因为它是在 HTML 中调用的,接下来会触发 API。任何人都可以帮助我使用 $watch 函数吗?还是我需要使用其他东西,以便在页面上触发 API,然后触发指令。

API 代码(为了代码的完整性而进行了 trim )

$timeout(function () {$http({
method: 'GET',
url: 'getPSDetails?psuid='+$scope.psuId,
//url: 'getPSDetails?controlNumber='+$scope.DataEntered,
}).success(function (data) {
console.log('success response'); }

$scope.summaryObject =data; ( I am getting all the data here )

我的指令。 (为了代码的完整性而进行了 trim )

myapp.directive('summaryFeatureDetails',function(){
return{
restrict: 'E',
scope:true,
controller:function($scope){

$scope.columnPerPage = 5;
$scope.currentPage = 0;
$scope.currentColumns = [];
$scope.controlData = [];
$scope.totalNumberOfPage = Math.floor($scope.selectedData.length/$scope.columnPerPage);
if (($scope.selectedData.length % $scope.columnPerPage) > 0){
$scope.totalNumberOfPage = $scope.totalNumberOfPage + 1;
}
}
}

最佳答案

如果指令在设置 $scope.summaryObject 之前加载,请确保在设置对象之后加载指令。只需添加 NgIf 即可完成此操作。指令标记上的表达式,用于检查对象值,并且仅在对象不为 null 时才渲染 html

<yourDirectiveTag ng-if="!summaryObject" ></yourDirectiveTag>

关于javascript - 如何在此处使用 $watch 以便在指令 USING ui-router 之前触发 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49392005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com