【问题标题】:Ui-Router for Angular - Partials htmlUi-Router for Angular - Partials html
【发布时间】:2017-10-20 12:44:42
【问题描述】:

那里!我正在尝试使用部分,但不工作:

我的 html 文件已创建并就位,我不明白为什么它不起作用!

我正在使用 angular 1.6.4 和 ui-router 1.0.0-rc.1

谁能帮帮我?

myApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state("home", {
url: "/home",
templateUrl: "home.html",
controller: "HomeController",
});
$stateProvider
.state("about", {
url: "/about",
templateUrl: "about.html",
controller: "AboutController",
});
$stateProvider
.state("contact", {
url: "/contact",
templateUrl: "contact.html",
controller: "ContactController",
});
});
myApp.controller('HomeController', function($scope, $location) {
$scope.message = 'Home Controller!';
});
myApp.controller('AboutController', function($scope) {
$scope.message = 'About Controller.';
});
myApp.controller('ContactController', function($scope) {
$scope.message = 'Contact Controller';
});

如果我更改为 this(如下),它就可以正常工作:

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

myApp.config(function($stateProvider) {
  var helloState = {
    name: 'hello',
    url: '/hello',
    template: '<h3>hello world!</h3>'
  }

  var aboutState = {
    name: 'about',
    url: '/about',
    template: '<h3>Its the UI-Router hello world app!</h3>'
  }

  $stateProvider.state(helloState);
  $stateProvider.state(aboutState);
});

【问题讨论】:

    标签: html angularjs angular-ui-router partials


    【解决方案1】:

    对于初学者,请检查这段代码以了解$stateProvider 的正确语法

    app.config(function($stateProvider) {
      $stateProvider
        .state('home', {
          url: '/home',
          templateUrl: 'views/home.html'
        }) // no semicolon here
        .state('projects', {
          url: '/projects',
          templateUrl: 'views/projects.html'
        }) // no semicolon here
        // etc
    });
    

    你不应该在你的州之间有;

    另外,你应该把它们放在一起,而不是把$stateProvider分开

    此外,您应该删除 $stateProvider 元素中每个控制器声明末尾的额外 ,

    这是一个工作示例。
    http://embed.plnkr.co/YKtubLucc5Qa4GwGI77V/

    【讨论】:

    • 嗨,瑞奇,非常感谢!我从代码中删除了额外的 $stateProvider、分号和“,”,但仍然无法正常工作。我也必须删除 $urlRouterProvider 吗?
    • 在我的用例中,我没有为每个状态使用特定的控制器。
    • 我不确定$urlRouterProvider,我之前只使用过$routeProvider
    • 老实说,不久前我还试用了 ui-router 1.0.0,但它对我不起作用。我使用了 0.3.2,它对我来说非常有效,所以也许你应该尝试 0.3.2 并确保它在那里工作,然后跳过?
    • 当然了!我该怎么做?正在搜索您的个人资料中的界面以执行此操作...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2017-01-14
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多