在AngularJS中使用.controller方法时,无法访问$location

前端之家收集整理的这篇文章主要介绍了在AngularJS中使用.controller方法时,无法访问$location前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在表单上使用ng-submit将数据推送到Firebase,一切都按预期方式运行.我想做的是同时改变观点.在提交按钮本身,我已经点击设置以使用$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,$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>
您没有将$location对象注入到控制器中.它列在您的功能参数中,但您忘记在该功能之前将其添加到列表中.
myApp.controller('CtrlName','$location',$location) {
        ...
    }]);

猜你在找的Angularjs相关文章