我有这个:
<ng-include src="'app/components/widgets/buttonbar.html'"></ng-include>
包括/客户路由和/客户/:ID路由的按钮。我可以动态添加操作的按钮。当我去客户时,我使用此服务接口添加按钮:
this.buttons = []
this.addButton = function(id, title, classes, href, action, sortOrder){
console.log(this.buttons)
var button = {};
var pushViable = true;
button.id = id
button.title = title
button.classes = classes
button.href = href
button.action = action
button.sortOrder = sortOrder
angular.forEach(this.buttons, function(index, sort){
if(button.sortOrder === sort){
toastr.error('Button Sort Order Exists')
pushViable = false;
}
})
if(pushViable === true){
this.buttons.push(button)
this.buttons = sortButtons(this.buttons)
$rootScope.$broadcast('buttonBar:update')
}
}
喜欢我的客户名单:
buttonSvc.addButton('newCustomer','New Customer', 'button small', '#','newCustomer()',0)
$scope.newCustomer = function(){
var customerModal = $modal.open({
templateUrl: 'app/customers/newCustomer.html',
controller: 'customerModalCtrl',
windowClass: 'small'
})
customerModal.result.then(function(){
$rootScope.$broadcast('customer:update')
})
}
(从github上的pineconellc使用UI-foundation的模态这样做)
buttonSvc.addButton('goBack','Go Back', 'button small', '#','toTheGrid()',2)
此按钮无效,此代码实际上是不起作用的:
$scope.toTheGrid = function(){
$state.go('customers.list')
}
如果我只制作一个常规按钮并使用功能正常。所以我有一个问题。
由于我现在放了一个赏金,如果您想查看更多代码,请询问,我将发布相关资源。
看两个按钮:
<button ng-click="foo()">click</button>
<button ng-click="{{'foo()'}}">click</button>
第一项工作,第二次工作。(生成的HTML相同)参见plunk:http://plnkr.co/edit/znfa6lgugmq0byw0977zh?p=preview原因很简单 - 在第二种情况下," foo()"被视为基本字符串。
因此,只需修改您的代码以将函数传递到AddButton即可。
我认为问题是您将'toTheGrid()'
作为字符串而不是函数传递。
我会尝试这样的不同的东西:
buttonbar.html
<div class="container">
<ul class="button-group" ng-controller="buttonBar">
<li ng-repeat="button in buttons track by button.sortOrder">
<a href="{{button.href}}" class="{{button.classes}}" ng-click="button.action()">{{button.title}}</a>
</li>
</ul>
</div>
customerdetail.js
buttonSvc.addButton('goBack','Go Back', 'button small', '#', $scope.toTheGrid, 2);
我从评论中注意到,您说您有一个动态的HTML标签,如下:
<a href="#" class="button small ng-click-active" ng-click="toTheGrid()">Go Back</a>
我认为HREF可能会阻止ng单击启动。您可以尝试将href ="#"从锚定标签中删除(默认情况下,它将调用dreventDefault)。或者,您可以在锚定标签上编写用于属性的自定义指令,在自定义指令中,请在单击事件上拨打dextdefault()