简体   繁体   中英

understanding routes and templates html in angularjs, simples example possible

I'm trying to embed angularjs into my existing asp.net mvc4 app.

(Views/Shared/) _Layout.cshtml

<html data-ng-app="myApp">
     <head>
       <script src="~/Scripts/angular.min.js"></script>
       <script src="/js/main.js"></script>     
     </head>
   <body>
        @RenderBody()
   </body>
</html>

/js/main.js

var app = angular.module("myApp", []);
module.config(function ($routeProvider) {
    $routeProvider.when("/topics", {
        controller: "topicsController",
        templateUrl: "/js/templates/topicsView.html"
    });
    $routeProvider.otherwise({ redirectTo: "/" });
});

app.controller("topicsController", function ($scope) {
    $scope.testData = "some dummy data";
});

/js/templates/topicsView.html

<h1>topics template view, dummy object </h1>
{{ testData }}

and inside /Views/Home/Index.cshtml

<div data-ng-view=""></div>

I'm trying to navigate to /topics using #/topics url but nothing is rendered (blank page, not 404), not even h1 title from templates/topicsView.html

What I'm doing wrong?

You forgot to include ngRoute module. Try this:

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

您需要在创建angular主应用模块时将angular-routes.js与angular.js包括在内,然后将ngRoutes模块作为依赖项包括在内。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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