gpt4 book ai didi

javascript - Angular : Calling function in factory from html ng-click

转载 作者:行者123 更新时间:2023-11-30 15:59:35 25 4
gpt4 key购买 nike

我正在尝试从 html 调用在工厂中声明的函数(myfunc):

<button type="button" class="btn btn-default btn-sm" ng-click="myfunc(search_name)">Search</button>

Controller 和工厂代码如下:

var angularjsapp = angular.module('graphApp', ['ngAnimate', 'ui.bootstrap']);
angularjsapp.factory('searchFactory', function() {
//return $resource('friends.json');
return{
myfunc:function(search_name){
console.log('ok')
keyword_type = 1
WebSocketTest(search_name,keyword_type)

}
}
});

angularjsapp.controller('AccordionDemoCtrl', function($scope,searchFactory) {
$scope.count = 0;
$scope.namesPerPage = 10
$scope.currentPage = 1;
$scope.searchFactory = searchFactory.myfunc
});

到目前为止,我无法在控制台中记录“ok”

最佳答案

您应该像这样调用分配给 searchFactory.myfuncscope 函数:

<button type="button" class="btn btn-default btn-sm" ng-click="searchFactory(search_name)">Search</button>

关于javascript - Angular : Calling function in factory from html ng-click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com