gpt4 book ai didi

angularjs - 根据条件禁用 ui-sref

转载 作者:行者123 更新时间:2023-12-03 06:28:29 25 4
gpt4 key购买 nike

你能告诉我一种禁用提交按钮的方法吗,该按钮会通过以下方式更改为新状态:

<a ui-sref="state">Submit</a>

仅当表单有效时才应启用该按钮。

ng-disabledui-sref 不起作用:

<form name="tickets">
<button ng-disabled="!canSave()"><a ui-sref="view">Submit</a></button>
</form>

app.js 中的 canSave 函数是:

$scope.canSave = function(){
return $scope.tickets.$dirty && $scope.tickets.$valid;
};

最佳答案

您可以简单地将其与 ng-click 配对,以便 ng-disabled 正常工作。

.controller('myCtrl', function($scope, $state) {
// so that you can call `$state.go()` from your ng-click
$scope.go = $state.go.bind($state);
})
<!-- call `go()` and pass the state you want to go to -->
<button ng-disabled="!canSave()" ng-click="go('view')>Submit</button>

这是使用自定义指令的更奇特的方法:

angular.module('myApp', ['ui.router'])
.config(function($stateProvider) {
$stateProvider.state('home', {
url: '/'
});
})
.controller('myCtrl', function() {

})
.directive('uiSrefIf', function($compile) {
return {
scope: {
val: '@uiSrefVal',
if: '=uiSrefIf'
},
link: function($scope, $element, $attrs) {
$element.removeAttr('ui-sref-if');
$compile($element)($scope);

$scope.$watch('if', function(bool) {
if (bool) {
$element.attr('ui-sref', $scope.val);
} else {
$element.removeAttr('ui-sref');
$element.removeAttr('href');
}
$compile($element)($scope);
});
}
};
})
;
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
<form name="form">
<input ng-model="foo" ng-required="true">
<button ng-disabled="form.$invalid">
<a ui-sref-if="!form.$invalid" ui-sref-val="home">test</a>
</button>
</form>
</div>

关于angularjs - 根据条件禁用 ui-sref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973530/

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