AngularJS联系人表单与当前的app.js合并
AngularJS contact form app merge with the current app.js
这是我当前的angular app.js,我希望它能与website.js一起工作。
'use strict';
// angular.js main app initialization
var app = angular.module('kaidoweb', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'pages/index.html',
activetab: 'index',
controller: HomeCtrl
}).
/* when('/mainpage/:mainpageId', {
templateUrl: function (params) { return 'pages/' + params.mainpageId + '.html'; },
controller: ProjectCtrl,
activetab: 'mainpage'
}).*/
when('/works', {
templateUrl: 'pages/works.html',
controller: PrivacyCtrl,
activetab: 'works'
}).
otherwise({ redirectTo: '/' });
}]).run(['$rootScope', '$http', '$browser', '$timeout', "$route", function ($scope, $http, $browser, $timeout, $route) {
$scope.$on("$routeChangeSuccess", function (scope, next, current) {
$scope.part = $route.current.activetab;
});
// save the 'Contact Us' form
$scope.save = function () {
$scope.loaded = true;
$scope.process = true;
$http.post('sendemail.php', $scope.message).success(function () {
$scope.success = true;
$scope.process = false;
});
};
}]);
app.config(['$locationProvider', function($location) {
$location.hashPrefix('!');
}]);
这是websiteApp获取联系人表单工作教程http://sarahcapecci.com/blog/processing-a-form-with-angularjs-php/
// creating Angular Module
var websiteApp = angular.module('websiteApp', []);
// create angular controller and pass in $scope and $http
websiteApp.controller('FormController',function($scope, $http) {
// creating a blank object to hold our form information.
//$scope will allow this to pass between controller and view
$scope.formData = {};
// submission message doesn't show when page loads
$scope.submission = false;
// Updated code thanks to Yotam
var param = function(data) {
var returnString = '';
for (d in data){
if (data.hasOwnProperty(d))
returnString += d + '=' + data[d] + '&';
}
// Remove last ampersand and return
return returnString.slice( 0, returnString.length - 1 );
};
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'process.php',
data : param($scope.formData), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function(data) {
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorName = data.errors.name;
$scope.errorEmail = data.errors.email;
$scope.errorTextarea = data.errors.message;
$scope.submissionMessage = data.messageError;
$scope.submission = true; //shows the error message
} else {
// if successful, bind success message to message
$scope.submissionMessage = data.messageSuccess;
$scope.formData = {}; // form fields are emptied with this line
$scope.submission = true; //shows the success message
}
});
};
});
我已经使用了<html ng-app="kaidoweb">
,它不允许我使用另一个ng-app。我试过它独立,它似乎工作,但我不能得到它与当前的应用程序的工作。
有没有办法合并它们或者其他标准的方法。如果有更好,更简单的方法来做到这一点,并且知道一些好的例子,我也会很感激的。
既然您已经创建了一个module
,那么这个:
var app = angular.module('kaidoweb', []);
您不需要再创建一个。你应该做的是使用你自己的,只使用教程中的FormController
。
另外,我不认为你需要使用注释//save the 'Contact Us' form
中的代码,因为你要用教程中的函数替换这个函数。
然后你可以把我说的控制器,插入到你的app
的末尾,然后按照剩下的。
我不是一个先进的AngularJS
用户,但据我所知,这应该工作。
相关文章:
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- Django无法通过urls.py配置找到djangular/app.js文件.如何进行故障排除
- 使用Express4将数组从Nodejs中的app.js传递到routes/index.js
- 从Babel 5.8.35升级到6.0.0将不会编译带有SyntaxError和意外标记的app.js
- Zendesk app.js示例用于发送外部电子邮件通知或AJAX请求
- Angularjs app.js getting URL
- 为Backbone.js源文件(amd-app.js)重新创建构建环境
- ionic框架中的app.js函数冲突
- Trigger $modal.open from app.js
- 在app.js之外调用app.locals
- 如何确保angularjs app.js中的同步加载
- Ionic 2 从子页面修改 app.js 中的变量
- 部分而不是 app.js 中的角度路由定义
- mongodb 的 collection.find() 在 Express app.js 文件中使用时出现问题
- 是否可以在没有app.js的情况下创建一个extjs应用程序的js文件(如构建版本)
- sails.js中app.js文件中的环境变量
- 我可以把一个参数从控制器传递到app.js吗
- app.js显示错误:TypeError:无法调用方法'查询'为null
- Rails 4 App-JS在开发模式下可以与Webrick合作,但不能与Passenger/Apache2合作
- 如何在expressjs中从html页面调用app.js函数