gpt4 book ai didi

javascript - 使用 vm 在 angularjs 中重定向

转载 作者:行者123 更新时间:2023-11-30 12:03:19 26 4
gpt4 key购买 nike

当我使用 angularjs 提交表单时,我试图重定向到不同的路径,但我收到一条错误消息

Cannot read property 'go' of undefined object

下面是我的表单代码

<div class="col-sm-4 col-sm-push-4 margin-top-30">
<form ng-controller="LogonController" data-ng-submit="vm.setDjName()" name="logonForm" novalidate>
<div class="form-group">
<input type="text" name="dj-name" data-ng-model="vm.djName" placeholder="Your DJ Name" class="form-control">
</div>
<button class="btn btn-primary btn-block" data-ng-click="vm.setDjName()" type="button">Login</button>
</form>
</div>

这是我的 Controller

(function () {
'use strict';
angular
.module('app.controllers')
.controller('LogonController', LogonController);

LogonController.$inject = [];

function LogonController ($state) {
var vm = this;
vm.setDjName = function () {
$state.go('/video');
}
}

})();

我将如何重组代码以使重定向工作?

最佳答案

我认为你必须注入(inject) $state 服务:

LogonController.$inject = ['$state'];

// or

angular
.module('app.controllers')
.controller('LogonController', ['$state', LogonController]);

看看angular dependency injection documentation

关于javascript - 使用 vm 在 angularjs 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36044785/

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