【问题标题】:AngularJS: How to create routes with ui-router for my applicationAngularJS:如何使用 ui-router 为我的应用程序创建路由
【发布时间】:2017-04-02 08:32:20
【问题描述】:

我的a 标记有问题 - 我有一个根据GET 变量显示数据的页面。

例如 - /foo.php?opt=1 将显示每个人将去的城市表 - /foo.php?city=4 有去 /foo.php?school=4 的学校表显示学生表等。

问题是它第一次工作 - 当我选择城市时,它会显示学校列表并更改 url,但是当我选择学校时,它会更改 URL 但我仍然看到城市表,并且只有如果我按 F5,它会显示学生表。

这是代码:

odinvite.php:

<?php
if (isset($_GET['city']))
{
    include "odbycity.php";
}
else if (isset($_GET['school']))
{
    include "odbyschool.php";
}
else
{
    include "odshowcities.php";
}
?>

odshowcities.php:

<div ng-controller="allcities">

    <button class="btn btn-info" ng-repeat="x in names">
        <a href="/odinvite.php?city={{x.areaid}}">
        {{x.areaname}}</a>
    </button>

</div>

odbyschool.php:

<div ng-controller="odbycity">
    <button class="btn btn-info" ng-repeat="x in names">
        <a href="/odinvite.php?school={{x.schoolid}}">
        {{x.school_name}}</a>
    </button>
</div>

MyAngular.js:

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

myApp.config(function( $locationProvider) {

    $locationProvider.html5Mode(true);
});

myApp.controller ('allcities', function ($scope, $http)
{
    $http.get("fetch_json_sql.php?option=1")
        .then(function (response)
        {
            $scope.names = response.data.result;
        });
    console.log($scope.names);
});

myApp.controller ('odbycity', function ($scope, $http, $location)
{
    $scope.cityid=$location.search().city;
    console.log($scope.cityid);
    $http.get("fetch_json_sql.php?option=2&cityid="+$scope.cityid)
        .then(function (response)
        {
            $scope.names = response.data.result;
        });

});

myApp.controller ('odbyschool', function ($scope, $http ,$location)
{
    $scope.schoolid = $location.search().school;
    console.log($scope.schoolid);
    $http.get("fetch_json_sql.php?option=4&schoolid="+$scope.schoolid)
        .then(function (response)
        {
            $scope.names = response.data.result;
        });

});

可能是什么问题?

我尝试对 URL 进行 100% 更改 - &lt;a href="www.google.com"&gt;link&lt;/a&gt;,但没有成功。只是更改了没有重定向的 URL。

谢谢!

【问题讨论】:

  • 请添加您的路由配置。
  • @lin 只需添加它.. 可以吗?
  • 嗯 m8,AngularJS 是用于 SPA 的。直接将您的路线与后端结合起来并不容易。您应该使用 ngRoute 或 uiRouter 而不让后端呈现您的模板。我仍然看不到/odinvite.php?schoolodinvite.php?city 的路由配置。您的问题是“为什么我的路线不起作用”。
  • 这就是我所有的代码......我刚刚开始学习 AngularJS。关于这条路线,我需要了解什么?
  • 这是我的解决方案...? - docs.angularjs.org/api/ng/directive/ngHref

标签: html angularjs get href


【解决方案1】:

您应该停止使用后端渲染模板。 AngularJS 用于 SPA。如果您需要后端提供的数据,请尝试实现 API,例如一个 RESTful API。您需要配置您的路线,例如在此runnable demo plnkr 中。它使用ui-router。请注意,这只是一个演示。您应该能够将您的逻辑放入该原型中。我使用一些虚拟数据准备了您需要的所有路线。只需在控制器中包含您现有的 API,就可以了。

var myApp = angular.module("myApp", ['ui.router']);

myApp.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.when("", "/main");

    $stateProvider
        .state("main", {
            url: "/main",
            templateUrl: "main.html"
        })
        .state("main.listSchools", {
            url: "/listSchools/:schoolId",
            templateUrl: "schools.html"
        }) 
        .state("main.listAreas", {
            url: "/listAreas/:areaId",
            templateUrl: "areas.html"
        });
});


myApp.controller('mainMenuController', function ($scope) {
    $scope.schools = [{
      schoolid: 1,
      name: 'Test School 1'
    },{
      schoolid: 5,
      name: 'Test School 5'
    },{
      schoolid: 11,
      name: 'Test School 11'
    }];
    $scope.areas = [{
      areaid: 3,
      name: 'Test area 3'
    },{
      areaid: 7,
      name: 'Test area 7'
    },{
      areaid: 19,
      name: 'Test area 7'
    }];
});



myApp.controller('listSchoolController', function ($scope, $state) {
  $scope.schoolId = $state.params.schoolId;
});


myApp.controller('listAreaController', function ($scope, $state) {
  $scope.areaId = $state.params.areaId;
});

【讨论】:

  • 如果没有 Route?顺便说一句 - 我开始阅读“app.config(function($routeProvider) { $routeProvider .when("/", { templateUrl"” - 一样吗?
  • 你可以伪造路由,但这不是 AngularJS 的用途。 ngRoute 是不同的路由提供者。我建议使用uiRouter,因为它更加灵活,并且为您提供了更多设计路线的选择。
  • 好吧..我需要做一些功课来理解你写的东西——尽管它看起来有点合乎逻辑。你有一些推荐的网站来解释这个吗?
  • 希望有一些 YouTube 但那也很好 :) 非常感谢!!
  • 顺便说一句 - 当我使用 Route 时 - 有可能我必须配置服务器吗?因为我使用的是免费的主机服务器而不是专用的..
猜你喜欢
  • 2015-11-21
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
  • 2020-03-17
相关资源
最近更新 更多