Angularjs 向javascript函数发送$scope
我试图将$scope从angular控制器发送到javascript函数,但在调用我的javascript函数(teste2)后,$scope始终未定义 我还需要将$http、growl和ngProgressFactory传递给我的javascript函数Angularjs 向javascript函数发送$scope,angularjs,Angularjs,我试图将$scope从angular控制器发送到javascript函数,但在调用我的javascript函数(teste2)后,$scope始终未定义 我还需要将$http、growl和ngProgressFactory传递给我的javascript函数 angular.module('geonosis').controller('ListagemPessoasController', function (DTOptionsBuilder, DTColumnBuilder, $http, $q
angular.module('geonosis').controller('ListagemPessoasController', function (DTOptionsBuilder, DTColumnBuilder, $http, $q, $scope, growl, ngProgressFactory, $window) {
$scope.progressbar = ngProgressFactory.createInstance();
var vm = this;
vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() {
$scope.progressbar.start();
var defer = $q.defer();
$http({
method: 'GET',
url: 'http://localhost:8082/pessoa/3',
headers: {
'Authorization': '328316ed-41dd-491d-b362-d80ec55d5ebd'
}
}).then(function successCallback(response) {
defer.resolve(response.data);
$scope.progressbar.complete();
}, function errorCallback(response) {
$scope.progressbar.complete();
growl.error("<b>Erro ao consultar pessoas</b>", {});
});
return defer.promise;
})
.withLanguage({
sUrl: '//cdn.datatables.net/plug-ins/1.10.15/i18n/Portuguese-Brasil.json'
});
vm.dtColumns = [
DTColumnBuilder.newColumn('nome').withTitle('Nome').withOption('width', '40%').withClass('text-center'),
DTColumnBuilder.newColumn('categoriaPessoa').withTitle('Categoria').withOption('width', '25%').withClass('text-center'),
DTColumnBuilder.newColumn('cidade').withTitle('Cidade').withOption('defaultContent', 'não informada').withOption('width', '25%').withClass('text-center'),
DTColumnBuilder.newColumn(null).withTitle('').withOption('width', '10%').withClass('text-center').notSortable()
.renderWith(function(data, type, full, meta) {
return '<a class="btn btn-primary btn-sm" onclick="teste2(' + data.idPessoa + ')">' +
' <i class="fa fa-trash-o"></i></a>'
+
'<button class="btn btn-primary btn-sm" onclick="edit2(' + data.idPessoa + ')">' +
' <i class="fa fa-pencil-square-o"></i></button>';
})
];
});
function teste2(id, $scope){
console.log(scope); //ALWAYS UNDEFINED!!
}
angular.module('geonosis').controller('listagempessoascocontroller',函数(DTOptionsBuilder、DTColumnBuilder、$http、$q、$scope、growl、ngProgressFactory、$window){
$scope.progressbar=ngProgressFactory.createInstance();
var vm=这个;
vm.dtOptions=DTOptionsBuilder.fromfnomise(函数(){
$scope.progressbar.start();
var defer=$q.defer();
$http({
方法:“GET”,
网址:'http://localhost:8082/pessoa/3',
标题:{
“授权”:328316ed-41dd-491d-b362-d80ec55d5ebd
}
}).then(函数成功回调(响应){
延迟.解决(响应.数据);
$scope.progressbar.complete();
},函数errorCallback(响应){
$scope.progressbar.complete();
咆哮错误(“Erro ao CONSOTAR pessoas”{});
});
回报、承诺;
})
.用语言({
sUrl:“//cdn.datatables.net/plug-ins/1.10.15/i18n/葡萄牙语Brasil.json”
});
vm.dtColumns=[
DTColumnBuilder.newColumn('nome')。withTitle('nome')。withOption('width','40%')。withClass('text-center'),
DTColumnBuilder.newColumn('categoriaPessoa')。withTitle('Categoria')。withOption('width','25%')。withClass('text-center'),
DTColumnBuilder.newColumn('cidade')。withTitle('cidade')。withOption('defaultContent','não informada')。withOption('width','25%')。withClass('text-center'),
DTColumnBuilder.newColumn(null).withTitle(“”).withOption('width','10%').withClass('text-center').notSortable()
.renderWith(函数(数据、类型、完整、元){
返回“”+
' '
+
'' +
' ';
})
];
});
功能测试2(id$范围){
console.log(scope);//始终未定义!!
}
如果我将teste放入angular escope,我会收到:“teste2”未定义“$scope是angular提供的javascript对象,为了访问$scope,您必须在应用程序模块控制器功能中使用它。编辑并将其移动到控制器功能:
angular.module('geonosis').controller('ListagemPessoasController', yourCtrlFunction($scope,...other_injections){
function teste2(id, $scope){
console.log($scope);
}});
您需要在
Controller
中创建teste2
方法来访问$scope
是的,这就是问题所在。但我更改了所有代码以遵循本教程:在controllera中尝试$scope.teste2=teste2
,您需要更改为控制台.log($scope)
&然后使用感谢您的回答选中已解决hamzox,我使用以下链接解决:
angular.module('geonosis').controller('ListagemPessoasController', yourCtrlFunction($scope,...other_injections){
function teste2(id, $scope){
console.log($scope);
}});