角度控制器无法正常工作

时间:2016-10-08 18:29:03

标签: angularjs

我正在尝试设置一个简单的角度项目,但是,在应该初始化角度控制器的那一刻,不要工作......

/ home路由正在运行(或者我认为是这样,因为模板正在显示) 但是,ng-repeat在输出代码中被“评论”。

这是我的main.js:

(function()
{

    angular.module('angularTest',['ngRoute'])
    .config(config)

    config.$inject=['$routeProvider'];  
    function config($routeProvider)
    {
            $routeProvider
            .when('/home', {
            templateUrl : '/home',
            controller  : HomeCtrl
        });

    }

    HomeCtrl.$inject=['$scope'];
    function HomeCtrl($scope)
    {   console.log('homectrl');
        $scope.init = init;

        function init()
        {

            $scope.names=['a','b','c'];
        }
        $scope.init();
    }

})() 

server.js

var express     = require('express');  
var app         = express();  
var mongoose    = require('mongoose');
var http        = require('http');
var path        = require('path');


var home    = require('./app/routes/views/homeRoute');




app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine','pug');


app.use(home);

app.use(express.static(__dirname+'/public'));
app.set('port',process.env.PORT || 8080);

http.createServer(app).listen(app.get('port'),function()
{
    console.log('listening '+app.get('port'));
});

这是我的home.pug

html(ng-app='angularTest')
    head
        body
            h1(ng-repeat='name in names')
script(src='/libs/angular/angular.min.js')
script(src='/libs/angular/angular-route.min.js')
script(src='/libs/angular/angular-sanitize.min.js')
script(src='/js/main.js')

和输出

<html ng-app="angularTest" class="ng-scope">
    <head>
    </head>
    <body><!-- ngRepeat: name in names -->
        <script src="/libs/angular/angular.min.js"></script>
        <script src="/libs/angular/angular-route.min.js"></script>
        <script src="/libs/angular/angular-sanitize.min.js"></script>
        <script src="/js/main.js"></script>
    </body>
</html>

homeRoute.js

var router=require('express').Router();


router.get('/home',function(req, res) 
{

    res.render('_shared/home.pug');

});

module.exports=router;

完整代码:https://github.com/federhico/AngularTest

1 个答案:

答案 0 :(得分:0)

$routeProvider中的templateUrl应该是这样的,附加.html,同时检查路径是否正确,

将控制器放置''

 function config($routeProvider)
    {
            $routeProvider
            .when('/home', {
            templateUrl : '/home.html',
            controller  : 'HomeCtrl'
        });

    }