gpt4 book ai didi

javascript - angularjs - $state.go 更改 url 但不加载 html 页面

转载 作者:行者123 更新时间:2023-12-01 03:18:38 26 4
gpt4 key购买 nike

我正在练习 angularJS 中的路由并遇到问题。我在网上查了很多方法,但都没有解决我的问题。你能帮我吗?

我在使用 $state.go 函数时遇到问题。它更改了 url,但未加载 html 文件。我查看了控制台,但没有出现错误

app.js

(function(){
var app = angular.module('myTrello',
['date-directives', 'ngRoute', 'ui.router']);

app.controller("mainCtrl", ['$scope', '$http', '$state', '$route', function($scope, $http, $state, $route){
$scope.title = "Welcome to my trello";
$http.get("http://quotes.rest/qod.json")
.then(function(response) {
let quoteInfo = response.data.contents.quotes[0];
$scope.quote = quoteInfo.quote;
$scope.author = quoteInfo.author;
});
$scope.go = function(path) {
$state.go(path, {});
};
}]);

app.controller("boardCtrl", function(){

});

// app.controller("routeCtrl", function($scope, $routeParams) {
// $scope.param = $routeParams.param;
// });

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'index.html',
controller: 'mainCtrl'
})
.state('boards', {
url: '/boards',
templateUrl: 'boards/board.html',
controller: 'boardCtrl'
})
// .state('/boards/:param', {
// url: '/boards/:param',
// templateUrl: 'index.html',
// //controller: 'boardCtrl'
// })
}]);
})();

index.html(重要部分)

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="bower-angular-route/angular-route.js"></script>
<script src="https://unpkg.com/angular-ui-router@1.0.3/release/angular-ui-router.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
<script src="date.js"></script>

<body ng-controller="mainCtrl">
<div class="container">
<div class="jumbotron">
<div class="title">
<h2 class="text-center">{{title}}</h2>
</div>
</div>
<div class="bg-1 text-white text-center">
<h3>Today's quote</h3>
<h4>{{quote}}</h4>
<h5>By: {{author}}</h5>
</div>
</div>
<button class="btn btn-info btn-lg boards" ng-click="go('boards')">My boards</button>
<div ng-view></div>
</body>

board.html

<html>
<h1>Boards</h1>
</html>

最佳答案

由于您使用的是ui-router,您需要使用ui-view指令而不是ng-view,因此更改代码作为

<div ui-view></div>

而不是

<div ng-view></div>

关于javascript - angularjs - $state.go 更改 url 但不加载 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45374360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com