gpt4 book ai didi

javascript - 使用 ui-sref 在按钮标签中触发状态

转载 作者:IT王子 更新时间:2023-10-29 03:22:32 31 4
gpt4 key购买 nike

使用 angular-ui-router 触发状态的优雅方式是什么?通过点击 <button>

我正在使用 ui-sref="main"虽然它与 <a> 一起使用,但它似乎不起作用尽管。

HTML:

<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.13/angular.js" data-semver="1.2.13"></script>
<script src="http://angular-ui.github.io/ui-router/release/angular-ui-router.js" ></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

<nav class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#">Brand</a>
</div>
<ul class="nav navbar-nav navbar-left">
<li>
<button class="btn btn-danger navbar-btn" ui-sref="main" >Button Submit</button>
</li>
<li>
<a class="btn btn-danger navbar-btn" ui-sref="main" >Anchor Link Submit</a>
</li>
</ul>
</nav>


</body>

</html>

Javascript:

var app = angular.module('plunker', ['ui.router']);

'use strict';

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {

$stateProvider
.state("main", {
url: "/main",
resolve: {
test: function() {
alert("Triggered main state");
return true;
}
}
});

}]);


app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});

链接:http://plnkr.co/edit/vMtLN0ncpqAfgM6S1Ng4?p=preview

最佳答案

将按钮包裹在 anchor 中,一切顺利:

<a ui-sref="main">
<button class="btn btn-danger navbar-btn" >Button Submit</button>
</a>

DEMO

关于javascript - 使用 ui-sref 在按钮标签中触发状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124663/

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