angular js - 更新角度材料开关更改事件的$ stateParams

时间:2016-08-08 12:52:35

标签: angularjs angularjs-scope angular-ui-router angular-material

我想知道在angular material switch

的更改事件后是否有更新<section id="app_controls"> <span style="margin-right: 10px;">Ro</span> <md-switch class="md-primary" ng-model = "data.lang" aria-label="Switch language" ng-true-value="'en'" ng-false-value="'ro'" ng-change="setLang(data.lang)" >En</md-switch> <span id="exit"></span> </div> </section> 参数的方法
.controller('homeController', ['$stateParams','$state', '$scope', 'PageProperties', 'loadMyData',
            function($stateParams, $state, $scope, PageProperties, loadMyData){     


    //set page elements 
    var properties = PageProperties.setProps("home", loadMyData);
    $scope.props = properties;
    $scope.lang;

    $scope.setLang = function(lang) {           
        $scope.message = lang;
      };
}])

控制器

.state('home', {
            url: '/home?lang',
            params: { lang: 'ro'},
            templateUrl: 'views/home/home.html',
            controller: 'homeController',//'homeController'
            resolve:{
                    //loadResources e doar o denumire, nu vreun key-word
                      loadResources: ['$ocLazyLoad', function($ocLazyLoad) {
                          // you can lazy load files for an existing module
                          //conteaza ordinea in care le scriu
                                 return $ocLazyLoad.load(['pageNavPropsService', 'homeCtrl','homeDirective']);
                       }],
                       loadMyData: ['$stateParams', 'GetDataService', function($stateParams, GetDataService){
                           //get innitial data for states
                           var path = '_global/views/services/json/' + $stateParams.lang + '_data.json';
                           return  GetDataService.getData(path);
                       }]
            }
        })      

route.js

$scope

setLang()函数内的lang外,我无法从控制器访问其他内容。

我想要实现的是有一个语言切换,它将更新状态参数cd C:\Program Files (x86)\Google\Chrome\Application start chrome.exe --incognito --window-position=0,0 --kiosk --check-for-update-interval=604800 "facebook.com" exit --check-for-update-interval= 7days we are restarting the PC every day so update never tiger. - 我希望它对状态改变的其他状态保持不变。此外,我想在参数更改后重新加载此状态。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

基本上,您可以在$ rootScope中引用语言。

更新您的代码,它可能如下所示:

控制器

.controller('homeController', ['$stateParams','$state', '$scope', 'PageProperties', 'loadMyData', '$rootScope', 
            function($stateParams, $state, $scope, PageProperties, loadMyData, $rootScope){     


    //set page elements 
    var properties = PageProperties.setProps("home", loadMyData);
    $scope.props = properties;


    $scope.setLang = function(lang) {           
        $rootScope.lang = lang;
      };
}])

route.js

.state('home', {
    url: '/home?lang',
    params: { lang: 'ro'},
    templateUrl: 'views/home/home.html',
    controller: 'homeController',//'homeController'
    resolve:{
            //loadResources e doar o denumire, nu vreun key-word
              loadResources: ['$ocLazyLoad', function($ocLazyLoad) {
                  // you can lazy load files for an existing module
                  //conteaza ordinea in care le scriu
                         return $ocLazyLoad.load(['pageNavPropsService', 'homeCtrl','homeDirective']);
               }],
               loadMyData: ['$stateParams', '$rootScope', 'GetDataService', function($stateParams, $rootScope, GetDataService){
                   //get innitial data for states
                   var path = '_global/views/services/json/' + $rootScope.lang + '_data.json';
                   return  GetDataService.getData(path);
               }]
    }
})

另外,要设置默认语言,您可以在app.run中存储语言的默认值。

答案 1 :(得分:0)

最后我用这样的服务完成了它:

.factory('langService', function() {
        var default_lang = "ro";
        var setLang = function(lang){
            if(!lang)
                this.language = default_lang;
            else
                this.language = lang;
            };
        var getLang = function(){   
            if(!this.language)
                return this.language = default_lang;
            else
                return this.language;
        }

        return {
            setLang : setLang,
            getLang: getLang
        }
    })

并处于州决心:

resolve:{
    loadResources: ['$ocLazyLoad', function($ocLazyLoad) {
                                 return $ocLazyLoad.load(['pageNavPropsService', 'gourmetCtrl' ,'customHeader', 'customFooter']);
    }],
    loadMyData: ['$stateParams', 'GetDataService', 'langService', function($stateParams, GetDataService, langService){
               var path = '_global/views/services/json/' + langService.getLang() + '_data.json';
               return  GetDataService.getData(path);
            }]
    }