Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将$scope添加到控制器_Javascript_Angularjs_Model View Controller - Fatal编程技术网

Javascript 将$scope添加到控制器

Javascript 将$scope添加到控制器,javascript,angularjs,model-view-controller,Javascript,Angularjs,Model View Controller,可能是个愚蠢的问题。。。但我想做最好的练习。所以我有我的控制器: (function() { 'use strict'; angular .module('example.cancha') .controller('CanchaController', CanchaController); CanchaController.$inject = ['$state', 'canchaService']; function CanchaController($state, can

可能是个愚蠢的问题。。。但我想做最好的练习。所以我有我的控制器:

(function() {
'use strict';

angular
    .module('example.cancha')
    .controller('CanchaController', CanchaController);

CanchaController.$inject = ['$state', 'canchaService'];

function CanchaController($state, canchaService) {
    var vm = angular.extend(this, {
        canchasComplejo: []
        });



    (function activate() {
        cargarCanchasComplejo();
    })();

    //funcion que llama al servicio para obtener las canchas del complejo
    function cargarCanchasComplejo() {
        canchaService.obtenerCanchasComplejo()
            .then(function(canchasComplejo) {
                vm.canchasComplejo = canchasComplejo;
        });
    }
}
})();
现在我需要调用一个函数并使用

为此,我需要使用$scope 如何在控制器中集成$scope?因为我最喜欢的控制器是:

.controller('MyCtrl', function($scope) {
    ... 
});

谢谢你的帮助

您可以使用注入$state的相同方式注入$scope,因此:

CanchaController.$inject = ['$state', '$scope', 'canchaService'];

function CanchaController($state, $scope, canchaService) { ...

您可以试试这个

您需要用$inject注入$scope

例:


阅读一些关于“角度控制器”的文章。您正在混合
$scope vs controllerAs的方法学请尝试回答我的问题(与本文相关):
CanchaController.$inject = ['$state', '$scope', 'canchaService'];

function CanchaController($state, $scope, canchaService) { ...
.controller('CanchaController',function($scope,$state, canchaService) {
    var vm = angular.extend(this, {
        canchasComplejo: []
        });

CanchaController.$inject = ['$scope','$state', 'canchaService'];
(function () {

    angular
        .module('example.cancha')
        .controller('CanchaController', CanchaController);

    CanchaController.$inject = ['$state', 'canchaService','$scope'];
    function CanchaController($state,canchaService,$scope) {
// you can call function here with $scope enter code here
      $scope.xyz = function(){

        }
    }
})();