我在表单上使用 ng-submit 将数据推送到 Firebase,一切都按预期工作。我想做的是同时改变观点。在提交按钮本身上,我设置了 ng-click 以使用 $location 执行函数。如果我将 changeView 函数放在 .controller 方法中,我将无法使用 $location(具体来说,它说 - “错误:'undefined' 不是对象(评估 '$location.path')”)。任何帮助都是超级骗子。
// This doesn't work and throws the error
myApp.controller('CtrlName', ['$scope', 'angularFireCollection',
function($scope, angularFireCollection, $location) {
$scope.changeView = function(view) {
$location.path(view);
}
}
]);
// This works as expected, but I'm name spacing my functions globally and I will have to change how I'm accessing my Firebase, which isn't really desired.
function CtrlName($scope, angularFireCollection, $location) {
$scope.changeView = function(view) {
$location.path(view);
}
}
这是我的模板:
<form role="form" ng-submit="tactics.add(tactic)">
<div class="form-group">
<label>Select Method</label>
<select class="form-control" ng-model="tactic.type">
<option>Email</option>
<option>Display</option>
<option>SMS</option>
<option>Print</option>
</select>
</div>
<button type="submit" class="btn btn-success" ng-click="changeView('/my-tactics')">Save</button>
</form>