我遇到一个按钮问题,他在DOM控制台中给了我一条错误消息。错误消息是:
TypeError:object不是函数 在angular.js:10822 在angular.js:19062年 at k。$ eval(angular.js:12658) at k。$ apply(angular.js:12756) 在HTMLFormElement。 (angular.js:19061) 在angular.js:2843 在r(angular.js:325) 在HTMLFormElement.c(angular.js:2842)
我正在使用AngularJS,这是我的HTML代码:
<!-- partial-contact.html -->
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div id="form-container">
<!-- Contact Heading -->
<div class="page-header text-center">
<h1><i class="fa fa-comment"></i> Kontakt</h1>
<p>Ved spørsmål skriv ein epost i kontaktskjemaet under.</p>
</div>
<!-- Form -->
<!-- use ng-submit to catch the form submission and use our Angular function -->
<form id="contactForm" name="contactForm" novalidate ng-submit="contactForm()" ng-controller="contactController" method="post">
<!-- Name -->
<div class="input-group margin-bottom-sm">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input id="personName" class="form-control" type="text" name="personName" ng-model="contactData.personName" placeholder="Namn *" required />
</div>
<!-- Email -->
<div class="input-group margin-bottom-sm">
<span class="input-group-addon"><i class="fa fa-envelope-o fa-fw"></i></span>
<input id="email" class="form-control" type="email" name="email" ng-model="contactData.email" placeholder="Epost *" required />
</div>
<!-- Subject -->
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-info"></i></span>
<input id="subject" class="form-control" type="text" name="subject" ng-model="contactData.subject" placeholder="Emne *" required />
</div>
<!-- Email Message & Button -->
<div class="input-group">
<div class="input-group-btn">
<!-- Email Message -->
<textarea id="emailMessage" type="text" class="form-control" name="emailMessage" ng-model="contactData.emailMessage" ng-minlength=10 ng-maxlength=300 placeholder="Melding..." required></textarea>
<!-- Button -->
<button id="submit" type="submit" class="btn btn-primary" ng-click="sendMessage('#/path/' + obj.val1 + '/' + 'obj.val2' + '/' + 'obj.val3' + '/' + 'obj.val4')"><i class="fa fa-angle-double-right"></i></button>
debug: {{debug.val}}
</div>
</div>
<br>
</form>
</div>
</div>
</div>
我不确定问题是否是-element中的ng-click方法,并且它不适用于-element中的ng-submit。任何人都可以在我的代码中看到任何其他错误吗?
这是我的控制器:
// contact controller
eventApp.controller('contactController', ['$scope', '$http', function($scope, $http) {
$scope.obj = {
val1: '$scope.personName',
val2: '$scope.email',
val3: '$scope.subject',
val4: '$scope.emailMessage'
};
$scope.debug = {
val: ''
};
$scope.sendMessage = function(input) {
$scope.debug.val = input;
};
}]);
我从这个链接中得到了灵感:
concat scope variables into string in angular directive expresssion
答案 0 :(得分:0)
如果你想以这种方式使用它,在函数sendMessage(obj)
中传递对象,然后在你的js中传递:
$scope.obj = {
val1: 'personName',
val2: 'email',
val3: 'subject',
val4: 'emailMessage'
};
$scope.sendMessage = function(obj) {
var string = '#/path/' + $scope[obj.val1] + '/' + $scope[obj.val2] + '/' + $scope[obj.val3] + '/' + $scope[obj.val4];
$scope.debug.val = string;
};
JavaScript objects
被视为arrays
,因此$scope[obj.val1]
== $scope.personName
,因为$scope.obj.val1
的价值。