angularjs - 使用从另一个 Controller ionic 更新的新内容更新侧边菜单

标签 angularjs angularjs-scope ionic-framework

我有显示用户个人资料图片和用户名的侧边菜单。当用户从我的设置页面更新这些信息时,它不会更新侧面菜单信息。

服务功能

//My service function
myApp.factory('User',function(Auth,dataService){

    var userInfo = null;

    return {
        getUserInfo:function(){
            if (userInfo == null){
                var loggedIn = Auth.isLoggedIn();
                var inputs = {auth_token:loggedIn.auth_token};
                console.log("Input User "+JSON.stringify(inputs));
                var promise = dataService.getUserInfo(inputs).then(function(response){
                    userInfo = response;
                    return response;
                });
                console.log("USERINFO IS "+ JSON.stringify(promise));
                return promise;
            }
            return userInfo;

        },
        refreshUserInfo:function(){
            var loggedIn = Auth.isLoggedIn();
            var inputs = {auth_token:loggedIn.auth_token};
            console.log("Input User "+JSON.stringify(inputs));
            var promise = dataService.getUserInfo(inputs).then(function(response){
                userInfo = response;
                return response;
            });

            return promise;
        }
    };
});

侧边菜单 View
 //Side menu View

     <ion-side-menu side="left"  id="leftSideMenu" class="menu_containor" style="visibility: hidden;" drag-content="false">

          <ion-content>
              <div>
                  <div class="about_top no_back">

                      <div class="menu_left_img">

                          <img src="{{userInfo.pic_url}}" width="110" height="110">
                      </div>
                      <!--<a href="#" class="red_icon"><img src="img/menu-redicon.png"  width="32"></a>-->
                      <div class="menu_right_txt">
                          <h3>Hello,</h3>
                          <h1>{{userInfo.user_firstname}}</h1>
                      </div>
                  </div>


              </div>

              </ion-content>


  </ion-side-menu>

Controller :
侧面菜单 Controller
//Side Menu Controller
appController.controller('AppCtrl', function($scope,$ionicPlatform, $ionicModal, $timeout,$state,User,definedVariable,$ionicSideMenuDelegate) {
    $ionicPlatform.ready(function() {

        //Get user information
        if (User.getUserInfo().length > 0){
            $scope.userInfo = User.getUserInfo()[0];

        }else{
            User.getUserInfo().then(function(response){
               $scope.userInfo = response[0];

            });
        }



    });

})

Controller :设置 Controller
//Settings Controller

.controller('settingsCtrl',['$scope','$stateParams','$ionicPlatform','$timeout','$ionicSideMenuDelegate','$ionicModal','Auth' ,
        '$location','$ionicActionSheet','countryList','User','definedVariable','dataService','adMobHelper',
        function($scope, $stateParams,$ionicPlatform,$timeout,$ionicSideMenuDelegate,$ionicModal,Auth,$location,$ionicActionSheet,countryList,User,definedVariable,dataService,adMobHelper) {
        $ionicPlatform.ready(function() {


            window.scope = $scope;


            $scope.userData = {};
            var user = User.getUserInfo();
            if (user.length >= 0){
                $scope.userData = user[0];
            }else{
                user.then(function(result){
                   $scope.userData = result[0];
                });
            }


            var modalOptions = {

                updateUserAction:function(){
                    console.log(dataService.get_auth_token().auth_token);
                    var loggedIn = Auth.isLoggedIn();
                    $scope.updated.auth_token = loggedIn.auth_token;
                    if (Auth.getPicId() != null){
                        $scope.updated.pic_id = Auth.getPicId();
                    }

                    Auth.update_user($scope.updated).then(function(result){

                       if (result){
                           var promise = User.refreshUserInfo().then(function(response){
                               $scope.modal.hide();
                           });
                           //$scope.modal.hide();
                       }else{
                           alert("Something went wrong");
                       }
                    });

                },

            };

            $scope.modalActions = modalOptions;

            $ionicModal.fromTemplateUrl('templates/editProfile.html', {
                scope: $scope
            }).then(function(modal) {
                $scope.modal = modal;
            });


        });
    }])

先感谢您

最佳答案

在我厂

$rootScope.$broadcast('user:updated',userInfo);

在我负责菜单内容的应用程序 Controller 中
$scope.$on('user:updated', function(event,data) {
            // you could inspect the data to see if what you care about changed, or just update your own scope
            $scope.userInfo = User.getUserInfo()[0];
        });

关于angularjs - 使用从另一个 Controller ionic 更新的新内容更新侧边菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27551856/

相关文章:

javascript - $scope.$watch 中的奇怪行为

angularjs - 当 AngularJS 中返回 JSON 时,使用 Resolve 显示模板

angularjs - 从指令模板调用服务方法

ios - ionic 2 后请求不起作用

angularjs - 使用 ng-class 加载指令时,类指令不起作用

javascript - 如何迭代 Angular 数据表中的过滤行

html - 如何在 CSS 中组合两个 Angularjs 表单验证指令条件?

angularjs - 如何在angularjs中点击调用两个函数

css - 基于 Angular 中的按钮单击执行不同的动画

ionic-framework - 如何获取ionic框架的版本?