简体   繁体   English

堆栈与角度路由器添加控制器

[英]Stack with angular router adding controller

Stack with something rather simple.I am using ngRoute for the first time and faced this. 与一些相当简单的东西堆叠。我第一次使用ngRoute并面对这个问题。

I've managed to get html includes working, but, attempting to add controller happens nothing. 我已经设法使html包括正常工作,但是,尝试添加控制器没有任何反应。

Js file: js文件:

var app = angular.module('admin', ['ngRoute'])

// Router config
app.config(function($routeProvider){
  $routeProvider
    .when('/', {
      templateUrl:"partials/index.html"
    })
    .when('/news', {
      templateUrl:"partials/news.html",
      controller:"newsCtrl"
    })
    .when('/changelog', {
      templateUrl:"partials/changelog.html"
    })
    .when('/todos', {
      templateUrl:"partials/todos.html"
    })
    .when('/users', {
      templateUrl:"partials/users.html"
    })
    .when('/messages', {
      templateUrl: "partials/messages.html"
    })
    .otherwise({
       redirectTo: '/'
    });
})

// Sidebar directive
app.directive('sidebar', function() {
  return {
    restrict: "E",
    templateUrl: "partials/sidebar.html"
  }
})

/* News Controller */
app.controller('newsCtrl', function($scope) {
  $scope.greeting = "HI"

  $scope.news = [
    "news 1",
    "news 2",
    "news 3"
  ]
})

And the html file (news.html): 和html文件(news.html):

<h3>News list:</h3>
<div class="content-container">
  {{ newsCtrl.greeting }}
  {{2+2}}
  <li ng-repeat="item in newsCtrl.news">{{item}}</li>
</div>

The output I receive is pretty much like this: 我收到的输出非常像这样:

News 新闻

4 4

I've tried attaching controller classical way - still no result. 我尝试附加控制器经典方式-仍然没有结果。 What am I missing? 我想念什么?

All you need is sth like this: 您所需要的只是这样:

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

myApp
.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'HomeController as homeCtl',
            templateUrl: 'partials/home.html'
        })
        .when('/about', {
            controller: 'AboutController as aboutCtl',
            templateUrl: 'partials/about.html'
        })
        .when('/contact', {
            controller: 'ContactController as contactCtl',
            templateUrl: 'partials/contact.html'
        })
        .otherwise({
            redirectTo: '/'
        });
})
.controller('HomeController', function() {
    var self = this;
    self.user = 'marios';
})
.controller('AboutController', function() {

})
.controller('ContactController', function() {

});

and the home template: home模板:

<div>
    <p>Hello {{homeCtl.user}}</p>
</div>

check here a working example or here . 在这里检查一个有效的例子或在这里

I assume you have included the <ng-view> directive in your main html file, so that NgRouter can replace it with each route's template. 我假设您在主html文件中包含了<ng-view>指令,以便NgRouter可以将其替换为每个路由的模板。

In your example you are using $scope . 在您的示例中,您正在使用$scope That means that you can refer to your controller's properties directly from your templates. 这意味着您可以直接从模板中引用控制器的属性。 ie: 即:

<li ng-repeat="item in news">{{item}}</li>

On the other hand, if you are going to use the Controller As syntax, you would do something like this: 另一方面,如果要使用Controller As语法,则应执行以下操作:

.when('/news', {
  templateUrl:"partials/news.html",
  controller:"newsCtrl",
  controllerAs: "vm"
})

app.controller('newsCtrl', function() {
  var vm = this;
  vm.greeting = "HI"

  vm.news = [
    "news 1",
    "news 2",
    "news 3"
  ]
})

<h3>News list:</h3>
<div class="content-container">
  {{ vm.greeting }}
  {{2+2}}
  <li ng-repeat="item in vm.news">{{item}}</li>
</div>

I recommend for you, to look the John Papas style guide, and you can make something like this to work 我为您推荐约翰·帕帕斯(John Papas)风格指南,您可以使类似的内容起作用

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

    app.config(routeConfig)
      .controller('newsCtrl', newsCtrl);
    // Router config
    routeConfig.$inject = ['$routeProvider'];
    function routeConfig($routeProvider){
      $routeProvider
        .when('/', {
          templateUrl:"partials/index.html"
        })
        .when('/news', {
          templateUrl:"partials/news.html",
          controller:"newsCtrl",
          controllerAs: "vm"
        })
        .when('/changelog', {
          templateUrl:"partials/changelog.html"
        })
        .when('/todos', {
          templateUrl:"partials/todos.html"
        })
        .when('/users', {
          templateUrl:"partials/users.html"
        })
        .when('/messages', {
          templateUrl: "partials/messages.html"
        })
        .otherwise({
           redirectTo: '/'
        });
    })

    // Sidebar directive
    app.directive('sidebar', function() {
      return {
        restrict: "E",
        templateUrl: "partials/sidebar.html"
      }

})

/* News Controller */
function newsCtrl () {
  var vm = this;

  vm.greeting = "HI"

  vm.news = [
    "news 1",
    "news 2",
    "news 3"
  ]
})

In html 在html中

<h3>News list:</h3>
<div class="content-container">
  {{ vm.greeting }}
  {{2+2}}
  <li ng-repeat="item in vm.news">{{item}}</li>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM