簡體   English   中英

使用controllerAs的angularJS路由

[英]angularJS routing using controllerAs

我是angularJS的新手,正在嘗試學習。 我有config.js文件,我在其中定義配置函數。 該文件本身的第一行給出了錯誤。

 angular.module('app').config(function($routeProvider, $locationProvider){

   $locationProvider.HTML5Mode(true);

   $routeProvider
   .when('/',{
       templateUrl: 'index.html',
       controller: 'MainController'
   })
   .when('/add', {
       templateUrl: 'add.html',
       controller: 'MainController',
       controllerAs: 'main',
   })

});

下面是我的app.js文件

angular.module('app',['ngRoute']);

在main.ctrl.js中,編寫了我的控制器函數。

angular.module('app').controller("MainController", function(){
var vm = this;
vm.title = 'TV Show';
vm.searchInput = '';
vm.shows = [
    {
        title: 'Game of Thrones',
        year: 2011,
        favorite: true
    },
    {
        title: 'Walking Dead',
        year: 2010,
        favorite: false
    },
    {
        title: 'Firefly',
        year: 2002,
        favorite: true
    },
    {
        title: 'Banshee',
        year: 2013,
        favorite: true
    },
    {
        title: 'Greys Anatomy',
        year: 2005,
        favorite: false
    }
];
vm.orders = [
{
    id: 1,
    title: 'Year Ascending',
    key: 'year',
    reverse: false
},
{
    id: 2,
    title: 'Year Descending',
    key: 'year',
    reverse: true
},
{
    id: 3,
    title: 'Title Ascending',
    key: 'title',
    reverse: false
},
{
    id: 4,
    title: 'Title Descending',
    key: 'title',
    reverse: true
}
];
vm.order = vm.orders[0];
vm.new = {};
vm.addShow = function() {
vm.shows.push(vm.new);
console.log("vm.shows");
vm.new = {};
};

});

現在,我創建了一個add.html頁面,試圖在其中訪問我的控制器(MainController),但無法訪問它。 你能告訴我我要去哪里了嗎

    <!DOCTYPE html>
    <html ng-app = 'app'>
    <head lang="en">
    <meta charset="UTF-8">
    <title>Favourite TV Show</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="main.ctrl.js"></script>
</head>
<body style="width: 60%; margin:0 auto;">
    <h1>This is add page.</h1>
    <h3>Add a new TV Show</h3>
    {{main.title}}
    {{main.shows[0].title}}
    <form name="main.addForm" class="form" ng-submit="main.addShow()">

        <div class="form-group">
            <label>Title</label>
            <input type="text" class="form-control" ng-model="main.new.title" required />
        </div>
        <div class="form-group">
            <label>Year</label>
            <input type="number" min="1900" max="2030" class="form-control" ng-model="main.new.year" required />
        </div>
        <div class="row">
            <div class="col-xs-6">
                <label>Favorite: <input type="checkbox" ng-model="main.new.favorite" /></label>
            </div>
            <div class="col-xs-6">
                <button class="btn btn-success pull-right"><span class="glyphicon glyphicon-plus-sign"></span> Add</button>
            </div>
        </div>
    </form>  
</body>
</html>

以下是我的index.html文件。 當用戶單擊新的顯示按鈕時,我將打開add.html頁面。

<body ng-app="app" ng-controller="MainController as main" style="width: 60%; margin:0 auto;">
    <div class="container">
        <h1>{{main.title}}</h1>
        <div class="input-group">

            <span class="input-group-addon">
                <span class="glyphicon glyphicon-search"></span>
            </span>
            <input type="text" class="form-control" ng-model="main.searchInput" style="width: 60%; margin:0 auto;">
        </div>
    <p>{{main.searchInput}}</p>
    </div>
    <a href=""><h3>A list of TV shows</h3></a>

    <ul class="list-group"> 
        <li class="list-group-item" ng-repeat="show in main.shows | filter:main.searchInput |  orderBy:main.order.key:main.order.reverse" >
            <span class="glyphicon glyphicon-star" ng-if="show.favorite"></span>
            {{show.title}} 
            <span class="badge">{{show.year}}</span>
            <i class="glyphicon glyphicon-edit"></i>
        </li>
    </ul>

    <select class="form-control pull-right" ng-model="main.order" ng-options="order as order.title for order in main.orders"></select>
    <div class="clearfix"></div>
    </br>
    <div class="row">
            <div class="col-xs-6"></div>
            <div class="col-xs-6">
                <a href="./add.html"><button class = "btn-primary pull-right">New Show</button></a>
            </div>
    </div>  

目前,您已經直接將內容添加到了體內。 我認為html應該屬於add.html 直接加載內容時,不會加載別名為main控制器MainController

當您想在ngRoute頁面上看到更改時,則需要在頁面上使用ng-view指令。 這將在頁面上動態添加add.html內容。

標記

<body style="width: 60%; margin:0 auto;">
    <ng-view></ng-view>
</body>

Add.html

<h1>This is add page.</h1>
<h3>Add a new TV Show</h3>
{{main.title}}
{{main.shows[0].title}}
<form name="main.addForm" class="form" ng-submit="main.addShow()">

    <div class="form-group">
        <label>Title</label>
        <input type="text" class="form-control" ng-model="main.new.title" required />
    </div>
    <div class="form-group">
        <label>Year</label>
        <input type="number" min="1900" max="2030" class="form-control" ng-model="main.new.year" required />
    </div>
    <div class="row">
        <div class="col-xs-6">
            <label>Favorite: <input type="checkbox" ng-model="main.new.favorite" /></label>
        </div>
        <div class="col-xs-6">
            <button class="btn btn-success pull-right"><span class="glyphicon glyphicon-plus-sign"></span> Add</button>
        </div>
    </div>
</form>

另外你應該在默認路由中有controllerAs: 'main'

$routeProvider
.when('/',{
    templateUrl: 'index.html',
    controller: 'MainController',
    controllerAs: 'main'
})

我已根據要求提供了更新的插件 請看看。 我注意到了一些事情,並提供了一些提示。:

避免全局

嘗試使用IIFE函數包裝模塊引用,而不是將其保留在全局名稱空間中。

angular.module("app", ["ngRoute"]; // Your code

建議

(function() {
    "use strict";
     angular.module("app", ["ngRoute"];
})();

$ locationProvider.html5Mode(true)而非$ locationProvider.HTML5Mode(true);

(function() {
    angular
        .module("app")
        .config(function($routeProvider, $locationProvider) {

           // $locationProvider.HTML5Mode(true); <-- your code
           $locationProvider.html5Mode(true);
           .... // rest of code
        });
})();

您嘗試使用config.js文件中的app變量來引用angular模塊,但是app從未定義,如果再次使用IIFE ,則可以引用它。

app.config(...) // app was not defined, had a reference error.

您可以像這樣引用它:

(function () {
    "use strict";

     angular
         .module("app")
         .config(function() {

         });
})();

請告訴我您是否要這樣做,我會根據需要不斷更新。 另外,我還提供了John Papa的樣式指南鏈接 ,該鏈接非常好,並得到Angular團隊的支持,請看一下。 我從中學到了很多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM