如何重新加载AngularJS部分模板?

8 javascript angularjs

在我的应用程序中,主模板有一个月份的下拉列表(Jan,Feb ...).

主模板包含一个ng-view,使用加载部分模板routeProvider.

如何ng-view从主模板的控制器刷新(通过重新运行其控制器)?

因此,当用户切换到其他月份时,部分模板内容将刷新.

主模板HTML:

....
<body class="" ng-controller="Main">
    <div ng-view></div>
</body>
Run Code Online (Sandbox Code Playgroud)

路线提供者:

....
.config([ '$routeProvider', function($route) {
  $route.when('/module/:module', {
    templateUrl : 'partial/module.html',
    controller : Module
  }).otherwise({
    templateUrl : 'partial/dashboard.html',
    controller : Dashboard
  });
} ]);
Run Code Online (Sandbox Code Playgroud)

主控制器:

function Main($scope, $cookies) {
    ...switch month and ajax...
    // Reload ng-view goes here
}
Run Code Online (Sandbox Code Playgroud)

小智 5

AngularJS广播功能在这里工作......

主模板控制器:

$scope.switch_month = function(new_month) {
  $.ajax({
    ....
    success : function() {
      $scope.$broadcast("REFRESH");
    },
    ....
  });
Run Code Online (Sandbox Code Playgroud)

};

每个部分模板控制器:

var refresh = function() {
  .... template initialization
};
refresh(); // initialize at once
$scope.$on("REFRESH", refresh); // re-initialize on signal
Run Code Online (Sandbox Code Playgroud)

  • 这与你原来的问题有什么关系? (2认同)