在AngularJS路由变化后,再次运行功能 [英] Running function again after route change in AngularJS

查看:133
本文介绍了在AngularJS路由变化后,再次运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个主控制器和另外两个人:

  calculatorApp.controller('companybController',函数($范围,$ HTTP){
  //定义硬件数据
  $ scope.customs.prodoc ='companyb';
});calculatorApp.controller('companyaController',函数($范围,$ HTTP){
  $ scope.customs.prodoc ='公司A';
});//创建控制器并注入角的范围$
calculatorApp.controller('mainController',函数($范围,$ HTTP){
  $ scope.customs = {
    prodoc:''
  };
});

但切换路由时 - 这个作品: {{} customs.prodoc} ,但似乎使用当新的看法是加载它不会再次运行该功能 - 导致该应用使用旧值

我如何运行 $ scope.customs.prodoc 再次看法已经改变?

使用不断变化的功能

下面是使用它的功能 - 我有它在 mainController ,但不知道它属于有

  $ http.get($ scope.customs.prodoc +'/ products.json')。成功(功能(thisdata){
        $ scope.products = []
        angular.forEach(thisdata,功能(产品){
            $ scope.products.push(产品);
        });
        的console.log($ scope.products);
    });$ scope.change =功能(){
//推荐产品
        $ scope.suggested = {}
        VAR长度=($ scope.products)。长度;
        对于(i = 0; I<长度;我++){
            如果($ scope.products [I] .CAMERA_MAX> = $ scope.totals.cameras){
                $ scope.suggested = $ scope.products [I]
                打破;
            }其他{            }        }
}


解决方案

我会用$ routeChangeSuccess做你正在谈论

什么

  $范围。在$($ routeChangeSuccess功能($ currentRoute,$previousRoute){
    $ scope.customs.prodoc = $ scope.routeParams.prodoc;
});

So I have a main controller and two others:

calculatorApp.controller('companybController', function($scope, $http) {
  //define the hardware data
  $scope.customs.prodoc = 'companyb';
});

calculatorApp.controller('companyaController', function($scope, $http) {
  $scope.customs.prodoc = 'companya';
});

// create the controller and inject Angular's $scope
calculatorApp.controller('mainController', function($scope, $http) {
  $scope.customs = {
    prodoc : ''
  };
});

But when switching routes - this works: {{customs.prodoc}}, but it seems the function that uses it does not run again when the new view is loaded - causing the app to use the old values.

How do I run the function that uses the changing $scope.customs.prodoc again once the view has been changed?

Here's the function that uses it - I had it in mainController but not sure it belongs there.

$http.get($scope.customs.prodoc+'/products.json').success(function(thisdata) {
        $scope.products = []
        angular.forEach(thisdata, function(product) {
            $scope.products.push(product);
        });
        console.log($scope.products);
    });

$scope.change = function() {
// Suggest product
        $scope.suggested = {}
        var length = ($scope.products).length;
        for(i=0; i<length; i++){
            if($scope.products[i].CAMERA_MAX>=$scope.totals.cameras){
                $scope.suggested = $scope.products[i];
                break;
            }else{

            }

        }
}

解决方案

I would use $routeChangeSuccess to do what you are talking about

$scope.$on("$routeChangeSuccess", function($currentRoute, $previousRoute) {
    $scope.customs.prodoc = $scope.routeParams.prodoc;
});

这篇关于在AngularJS路由变化后,再次运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆