首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用节点表达式的角控制器

不使用节点表达式的角控制器
EN

Stack Overflow用户
提问于 2016-09-05 17:13:35
回答 1查看 497关注 0票数 0

因此,我创建了一个角应用程序(1.5.7版),并希望将其部署到heroku。

我不得不实现Node,并使用express为heroku提供主index.html文件来构建它,因为它不接受普通的角度应用程序。然而,当我这样做的时候,我的两个控制器都坏了。我的联系人表单控制器没有呈现我的ng消息,我的页脚控制器根本就没有呈现过。

在控制台中,我得到一个错误:"ng:areq参数'contactCtrl‘不是函数,是未定义的“

您可以在这里查看这个项目的实时构建-https://fathomless-scrubland-50887.herokuapp.com和my https://github.com/StephenGrable1/AngularJS-Single-Page

这是我的特快server.js文件

代码语言:javascript
复制
var express = require('express');
var app = express();

var port = process.env.PORT || 8080;

app.use(express.static(__dirname));

app.get('/', function(req, res) {

    res.sendfile('index.html', {root: __dirname })
});

app.listen(port, function() {
  console.log('Our app is running on port ' + port);
});

这两个控制器在我实现节点和表达式之前工作得很好。

footer.js

代码语言:javascript
复制
angular
    .module('Single-Page-App')
    .directive('appFooter', function () {
        return {
            restrict: 'E',
            template: '© Name {{ getYearCtrl.date | date:"yyyy" }}',
            controller: function(){
                this.date = Date.now();
            },
            controllerAs:'getYearCtrl'
        };
    });

contactCtrl.js

代码语言:javascript
复制
angular
.module('Single-Page-App')
.controller('contactCtrl', ['$scope', '$http', function($scope, $http){
        $scope.contact = {name : '', email : '', subject : '', message : ''};


        $scope.submitForm = function() {
            var config = {
                method: 'POST',
                url : '../php/process-form.php',
                data : {
                    'name' : $scope.contact.name,
                    'email': $scope.contact.email,
                    'subject': $scope.contact.subject,
                    'message' : $scope.contact.message
                }
            };
            var request = $http(config);
            request.then(function (response){

                if(typeof(response.data) == 'string') {
                    // make all error messages blank when
                    // php return a string (which is the success message)
                    // which means there are no error messages being sent from php
                    $scope.nameError = "";
                    $scope.messageError = "";
                    $scope.subjectError = "";
                    $scope.emailError = "";

                    // put the success string from php into
                    // the successMsg so it can be accessed in the view
                    $scope.successMsg = response.data;

                    // clear all form values
                    // and set the inputs to prisitine and untouched
                    // so that angular will not display any error messages
                    // once a user submits the form successfully

                    $scope.contact = {};
                    $scope.contactForm.$setPristine();
                    $scope.contactForm.$setUntouched();

                    console.log($scope.successMsg);
                    console.log("not an object");
                } else {
                    if(typeof(response.data) == 'object') {
                    // if php sends an object
                    // (which contains all the error messages present)
                    // populate variables with error messages

                    $scope.nameError = response.data['name-error'];
                    $scope.messageError = response.data['message-error'];
                    $scope.subjectError = response.data['subject-error'];
                    $scope.emailError = response.data['email-error'];

                    //clear the success message if errors come back from php
                    $scope.successMsg = "";

                    console.log("it is an object");
                    }
                }


            }, function (error){
                $scope.msg = error.data;
                console.log($scope.msg);

        });
    }
}]);

我不知道是什么破坏了我的控制器的功能。

编辑:这是我的带有路由的main.js文件

代码语言:javascript
复制
var app = angular.module('Single-Page-App', ['ui.router', 'ngMessages']);

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');
    $stateProvider
    .state("home", {
        url:"/home",
        views: {
            "main@": {
                templateUrl: "partials/home.html"
            }
        }
    })
    .state("listen", {
        url:"/listen",
        views: {
            "main@": {
                templateUrl: "partials/listen.html"
            }
        }
    })
    .state("watchHere", {
        url:"/watch",
        views: {
            "main@": {
                templateUrl: "partials/watch.html"
            }
        }
    })
    .state("contact", {
        url:"/contact",
        views: {
            "main@": {
                templateUrl: "partials/contact.html"
            }
        }
    })
}])

angular.bootstrap(document, ['Single-Page-App']);
EN

回答 1

Stack Overflow用户

发布于 2016-09-05 17:52:22

使用

代码语言:javascript
复制
angular.module('Single-Page-App')
.controller('contactCtrl ',...[])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39335123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档