Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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 范围更改后重新加载控制器(AngularJS)_Javascript_Angularjs_View_Routes_Scope - Fatal编程技术网

Javascript 范围更改后重新加载控制器(AngularJS)

Javascript 范围更改后重新加载控制器(AngularJS),javascript,angularjs,view,routes,scope,Javascript,Angularjs,View,Routes,Scope,所以 我有一个用于交换语言的控制器 app.controller("langCtrl",['$scope','$route',function($scope,$route){ this.swap_lang = function(){ if(lang == "tr"){ lang = "en"; }else{ lang = "tr"; } console.log(lang);

所以 我有一个用于交换语言的控制器

app.controller("langCtrl",['$scope','$route',function($scope,$route){
    this.swap_lang = function(){
        if(lang == "tr"){
            lang = "en";
        }else{
            lang = "tr";
        }
        console.log(lang);
        this.lang = lang;
        //$route.reload();
        //$scope.$apply();
    };
}]);
下面这个应该负责根据全局变量语言打开菜单(带有短语言代码的JSON文件)

var lang = "en";
app.controller("menusCtrl",['$http','$scope',function($http,$scope){
    $scope.test = lang;//for testing
    var these_menu =  this;
    these_menu.links = [];
    these_menu.titles = "";
    $http.get("menu-" + lang + ".json").success(function(data){
        these_menu.links = data[0].menus;
        console.log("Menus are here!");
        console.log(these_menu.links[2].sub_menus[1]);

    });
}
lang变量交换,但未刷新菜单trl:(

如何用新数据更新视图,如何让控制器用新数据重新加载视图,我尝试了重新加载应用,但没有成功

PS:控制台在从中单击后打印简短的语言代码 看法


Angular无法很好地处理外部变量。请将lang变量设置为值提供程序:

app.value("lang","en");
然后最好定义一个工厂来处理语言交换,如下所示:

app.factory("langFactory",['$scope','$route', 'lang', function($scope, $route, lang){
    this.swap_lang = function(){
        lang == 'tr' ? 'en' : 'tr';
        console.log("lang",lang);
        $scope.$digest;
    };

    this.lang = function(){
        return lang;
    };

    return this;
}]);
然后像这样使用它,并在测试变量上添加一个watch,以便在发生更改时进行更新

app.controller("menusCtrl",['$http', '$scope', 'langFactory', function($http, $scope, langFactory){
    $scope.test = langFactory.lang;//for testing
    var these_menu =  this;
    these_menu.links = [];
    these_menu.titles = "";

    $scope.$watch("test",function(lang){
        $http.get("menu-" + lang + ".json")
            .success(function(data){
                these_menu.links = data[0].menus;
                console.log("Menus are here!");
                console.log(these_menu.links[2].sub_menus[1]);
            });
    });

}

首先,我会将其分解为一项服务,供您在整个应用程序中使用$http。如下所示:

var demo = angular.module('demo', []);

demo.service('GetData', function() {
  this.returnJsonByLangType = function(lang) {
    /** 
    *  normally I would use a function like 
    *  this to do the http work:
    *
    *  var response = $http.getJson(lang+'/dir/file.json');
    * Instead this will just alert the value with each change, 
    * ...you get the idea
    *
    **/

    alert('Current lang is: '+lang);

    // return response;

  }
});

 // Then I would instantiate a new service call returning the json based on the scope value which is changed via user selection of a link in the case below:

demo.controller('demoCtrl', function($scope, GetData) {
  $scope.languages = ['english', 'spanish', 'french'];
  $scope.activeLangSelection = $scope.languages[0]; // Default
  $scope.setLang = function(lang) {
    $scope.activeLangSelection = GetData.returnJsonByLangType(lang);
  };
});
PS:“这”的意思不是指$scope,它应该在那里维护控制器中的所有数据……也许你也想看看这个


这是笔,但是$http将不起作用,除非我有API代码返回基于lang的数据,但这应该可以让你达到你需要的位置。

也许你应该看看你可以声明json文件,然后用$translate.use(key)交换语言谢谢你的建议,我会考虑使用它在未来的应用程序,但我想一个简单的解决这个问题:)它提出了这一个