如何在AngularJs 1.7.x中从一个控制器调用方法到另一控制器,以刷新UI数据

时间:2018-07-01 04:31:20

标签: angularjs

   at Microsoft.SharePoint.Client.SharePointOnlineCredentials..ctor(String username, SecureString password)
   at JLL.PDS.Data.SharePoint.SharePointOnlineManager.GetCredentials() in C:\SourceCode\PDS Portal\JLL.PDS\JLL.PDS.Data.SharePoint\SharePointOnlineManager.cs:line 71
   at JLL.PDS.Data.SharePoint.SharePointOnlineManager.GetClientContext(String siteUrl) in C:\SourceCode\PDS Portal\JLL.PDS\JLL.PDS.Data.SharePoint\SharePointOnlineManager.cs:line 57
   at JLL.PDS.Data.SharePoint.SharePointUserProfileManager.Initialize() in C:\SourceCode\PDS Portal\JLL.PDS\JLL.PDS.Data.SharePoint\SharePointUserProfileManager.cs:line 43
   at JLL.PDS.Data.SharePoint.SharePointUserProfileManager.GetProfileImageData(Profile profile) in C:\SourceCode\PDS Portal\JLL.PDS\JLL.PDS.Data.SharePoint\SharePointUserProfileManager.cs:line 70
   at JLL.PDS.Model.Services.ProfileSyncService.<SaveProfilesToTargetDataSource>d__6.MoveNext() in C:\SourceCode\PDS Portal\JLL.PDS\JLL.PDS.Model\Services\ProfileSyncService.cs:line 106

上面是我的sideBar.html文件,我在其中循环coApplicantList,此列表填充在sideBarCtrl.js中。

<style>
.sidebar .logo img {
    display: none;
}

.sidebar.active .logo img {
    display: inline-block;
}

.sidebar.active .logo .first-letter {
    display: none !important;
}

.sidebar .logo .first-letter {
    display: block;
}

.first-letter {
    margin: 0;
    font-size: 30px;
    color: red;
}

.logo {
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
<div class="logo text-center">
    <img src="assets/img/VfinanceSLogo.png" width="150">
    <h1 class="first-letter">V</h1>
</div>
<div class="sidebar-wrapper"
    data-ng-if="user.userType == Constant.UserType.BORROWER.id">
    <ul class="nav">
        <li data-ui-sref-active="active"><a href="javascript:void(0)"
            data-ui-sref="web.lams.brDashboard"> <i class="material-icons">dashboard</i>
                <p class="item-name">Dashboard</p>
        </a></li>
        <li data-ui-sref-active="active"><a href="javascript:void(0)" data-ui-sref="web.lams.applicationList"> <i class="material-icons">Applications</i>
        <p class="item-name">Applications</p></a></li>

        <li data-ng-repeat="coApplicant in coApplicantList" data-ui-sref-active="active">
         <a href="javascript:void(0)" data-ui-sref="web.lams.coApplicants({id : coApplicant.id, parentUserId : coApplicant.parentUserId})">
         <i class="material-icons">{{ coApplicant.id }}</i> <p class="item-name">{{coApplicant.firstName}}</p></a>
         </li>
    </ul>
</div>



<div class="sidebar-wrapper"
    data-ng-if="user.userType == Constant.UserType.LENDER.id">
    <ul class="nav">
        <li data-ui-sref-active="active"><a href="javascript:void(0)"
            data-ui-sref="web.lams.products"> <i class="material-icons">dashboard</i>
                <p class="item-name">Dashboard</p>
        </a></li>
    </ul>
</div>


<div class="sidebar-wrapper"
    data-ng-if="user.userType == Constant.UserType.USER_TYPE.id">
    <ul class="nav">

        <li data-ui-sref-active="active"><a href="javascript:void(0)"
            data-ng-click="go()"> <i class="material-icons">Clients</i>
                <p class="item-name">Clients</p>
        </a></li>
    </ul>
</div>
<script>
    $('.sidebar').hover(function() {
        $('.sidebar').toggleClass('active');

    }, function() {
        $('.sidebar').toggleClass('active');

    })
</script>

上面是我的sideBarCtrl.js文件,在其中填充“ coApplicantList”并在sideBar.html上呈现

angular.module("lams").controller("sideBarCtrl",["$scope","$rootScope","Constant","applicationService",
        function($scope,$rootScope,Constant,$state,Notification, applicationService) {

    $scope.coApplicantList = [];

    $scope.getSideBarMenus = function() {
        applicationService.getCoApplicants().then(
            function(success) {
                console.log("getSideBarMenus :: success");
                if (success.data.status == 200) {
                    $scope.coApplicantList = success.data.data;
                } 
            }, function(error) {});
    }
    $scope.getSideBarMenus();
}]);

上面是我的其他控制器('coApplicantProfileCtrl.js'),我在其中添加了将无缝附加到sideBar菜单的选项。 我想做的是将sideBarCtrl.js注入到我的'coApplicantProfileCtrl'控制器中,但这在其他地方坏了。我的目标是刷新在'coApplicantProfileCtrl.js'中声明的createNewCoApplicant()成功方法内的侧边栏菜单项。

我尝试在sideBarCtrl.js文件中使用factory,但是这也无法在coApplicantProfileCtrl控制器内部进行注入。

1 个答案:

答案 0 :(得分:1)

尝试$emit,它将发出一个事件

angular.module("lams").controller("coApplicantProfileCtrl", function($scope, $http,$rootScope ) {
    $scope.documentResponse = {};

    $scope.createNewCoApplicant = function(){
        console.log("createNewCoApplicant");
        userService.creatCoApplicantProfile($scope.userData).then(
                function(success) {
                    $scope.isDisable = false;
                    if(success.data.status == 200){
                        Notification.info("Co-Applicant Added Successfully !");
                        $uibModalInstance.close('closed');
                        applicationService.getSideBarMenus();
                        $rootScope.$emit('refresh-sidebar',data_if_any);

                    }else{
                        Notification.error(success.data.message);
                    }
                }, function(error) {
                    $scope.isDisable = false;
                    $rootScope.validateErrorResponse(error);
         });        
    }
});

并在 sideBarCtrl

angular.module("lams").controller("sideBarCtrl",["$scope","$rootScope","Constant","applicationService",
        function($scope,$rootScope,Constant,$state,Notification, applicationService) {

    $scope.coApplicantList = [];

    $scope.getSideBarMenus = function() {
        applicationService.getCoApplicants().then(
            function(success) {
                console.log("getSideBarMenus :: success");
                if (success.data.status == 200) {
                    $scope.coApplicantList = success.data.data;
                } 
            }, function(error) {});
    }

    $rootScope.$on('refresh-sidebar',function(event,data){
        // you can access 'data_if_any' as 'data' variable
        $scope.getSideBarMenus();
    })

    $scope.getSideBarMenus();
}]);