Ng-include禁止运行路由



我在AngularJS应用程序中有一个非常奇怪的行为:我使用$routeProvider注册路由。除非我在 ng-view之前嵌入一个ng-include元素,否则效果很好。

目前我有两个选项来修复它:

  • ng-include元素移动到ng-view元素之后的位置
  • ng-include包裹在<div>元素中

你知道是什么导致了这个问题吗?

试试

    <div ng-controller="MainCtrl" ng-include src="template">
    angular.module('App')
        .controller('MainCtrl',['$route','$scope', function($route,$scope){
                $scope.template= 'main.html';
        }]);
In main.html, place the ng-view tag.

在这种情况下,你需要在控制器中处理ng-include,并注入$route作为依赖项。

$route也有一些问题。解决我的问题的一个方法是用ng-switch代替ng-viewangularjs中的条件ng-include

最新更新