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(){
}
}
})();