[英]AngularJS call function from click
I can't for the life of me figure out why I can't call this function in my controller.我一生都无法弄清楚为什么我不能在控制器中调用这个函数。
On clicking an accordion-group
attribute I get this error:单击
accordion-group
属性时,我收到此错误:
Uncaught ReferenceError: getConversationsForUser is not defined
Here is the html:这是html:
<ui-view id="smtConvoCard" layout="column" layout-fill layout-padding>
<div layout="row" flex layout-align="center center">
<md-card flex-gt-sm="90" onresize="resize()" flex-gt-md="80">
<md-card-content>
<md-list>
<h2>Conversations</h2>
<accordion close-others="oneAtATime">
<accordion-group heading="{{contact.FirstName}} {{contact.LastName}}" ng-repeat="contact in contacts" onclick="getConversationsForUser(contact.UserUID)">
<div>Test</div>
</accordion-group>
</accordion>
</md-list>
</md-card-content>
</md-card>
</div>
</ui-view>
Here is the controller being used (partial code):这是正在使用的控制器(部分代码):
controller('convCtrl', ['$scope', 'messageFactory', function ($scope, messageFactory) {
var currentUser = helpers.storage.get('UID');
$scope.contacts = [];
$scope.getContacts = function () {
/*Does stuff*/
};
//This is the function it is trying to call
$scope.getConversationsForUser = function (userUID) {
/*Does stuff*/
};
//Setup
$scope.getContacts();
}]);
I've tried changing the onclick
even to a different element, calling the getContacts
function instead and I always get Uncaught ReferenceError
我已经尝试将
onclick
更改为不同的元素,而是调用getContacts
函数,但我总是得到Uncaught ReferenceError
I know that function is within my scope because I'm data binding the contacts
variable to the page.我知道该函数在我的范围内,因为我正在将
contacts
变量数据绑定到页面。
Try :尝试 :
<accordion-group heading="{{contact.FirstName}} {{contact.LastName}}" ng-repeat="contact in contacts" ng-click="getConversationsForUser(contact.UserUID)">
<div>Test</div>
</accordion-group>
And don't forget to insert a ng-controller="convCtrl"
in an element wrapping your accordion
.并且不要忘记在包裹您的
accordion
的元素中插入一个ng-controller="convCtrl"
。
UPDATE更新
I add a little explanation : the $scope in a controller MySuperController
have to be linked in the DOM via a ng-controller="MySuperController"
attribute added to a tag (= a DOM element), so that your $scope variable will represent any variable within this controller.我添加一点解释:控制器
MySuperController
的 $scope 必须通过添加到标签(= DOM 元素)的ng-controller="MySuperController"
属性链接到 DOM 中,以便您的 $scope 变量将代表任何此控制器中的变量。
As soon as you create a function attached to this scope ( $scope.myFunction = function () { /* ... */ }
), you can then call it, ie, when an event is fired.一旦您创建了一个附加到此作用域的函数(
$scope.myFunction = function () { /* ... */ }
),您就可以调用它,即在触发事件时调用它。 This is the case via the ng-click
directive that attaches an event handler to the element on which it's set as an attribute.这是通过
ng-click
指令将事件处理程序附加到将其设置为属性的元素的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.