我的应用程序读取条形码后尝试单击搜索按钮,但到目前为止尚未成功。
我的搜索按钮是$scope.buscar
如何在 $('#txtCode').val(imageData.text);
之后执行此操作?
条形码Ctrl
.controller("BarCode", function( $scope, $cordovaBarcodeScanner){
$scope.scanBarcode = function(){
$cordovaBarcodeScanner.scan().then(function(imageData){
$('#txtCode').val(imageData.text);
}, function(error){
console.log('Deu Merda'+error);
});
}
})
人员和公司控制
.controller('PeopleCtrl',function($scope, $http) {
$scope.people = [];
angular.element(document).ready(function() {
var httpRequest = $http({
method: 'GET',
url: 'json/clientes.json',
}).success(function(data, status) {
$scope.people = data;
});
});
})
.controller('CtrlCorpo',function($scope, $http) {
$scope.people = [];
angular.element(document).ready(function() {
var httpRequest = $http({
method: 'GET',
url: 'json/clientes.json',
}).success(function(data, status) {
$scope.people = data;
});
});
搜索按钮
$scope.buscar = function (){
angular.forEach($scope.people, function(c, i) {
var item = "<td><a href=cardapio.html?"+c.id+">"+ c.id + "</a></td>" + "<td>"+ c.name +"</td>"
if(c.id == $('#txtCode').val()){
$('#tabelaPessoa').html(''),
$('#tabelaPessoa').append(item);
}
}
);
}
});
我必须在 JSON
中搜索所有客户端并开始显示我的应用程序。
当我单击 BarCodeScanner 按钮时,我将读取 ID,并自动搜索 ID 是否存在。在第二页自动输入'cardapio.html?clientid'。现在我正在尝试读取条形码后进行搜索。
最佳答案
您的问题是您正在尝试访问 Controller 外部的范围。
My button is under the control of
CtrlCorpo
which in turn is under the control Barcode.
在 Angular 中,Parent
Controller 无法修改Child
Controller 的数据。但子 Controller 确实具有访问权限,并且可以通过使用 $scope.$parent.varName
修改父 Controller 的作用域变量。有一篇关于此的很棒的文章Here 。
您正在尝试访问外部 Controller 内的作用域变量。看看我整理的示例,它复制了您遇到的问题。
var app = angular.module("MyApp", []);
app.controller("OuterCtrl", function($scope){
$scope.myOuterCtrlVariable = "OuterCtrl";
});
app.controller("InnerCtrlOne", function($scope){
$scope.ok = function(data){
$scope.myOuterCtrlVariable = data;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp">
<div ng-controller="OuterCtrl">
OUTSIDE: {{myOuterCtrlVariable}}
<div ng-controller="InnerCtrlOne">
INSIDE: {{myOuterCtrlVariable}}
<button ng-click="ok('From Inner Ctrl')">OK</button>
</div>
</div>
</div>
要解决此问题,您可以在 textfield
接收处理所需的数据量时$broadcast
一个事件。该事件将沿着 $scope
传播并命中 Button
所在的 ChildCtrl
;然后您可以使用从上面(上面是父 Controller )传递给它的数据来执行按钮。
这里值得注意的是,使用 $broadcast
会将事件名称向下调度到所有子作用域,而使用 $emit
则会使其向上调度。
阅读here有关 $emit
与 $broadcast
的更多信息。
查看此示例,了解如何使用 $broadcast
实现此目的。
function firstCtrl($scope)
{
$scope.$broadcast('someEvent', [1,2,3]);
}
function secondCtrl($scope)
{
$scope.$on('someEvent', function(event, mass) { console.log(mass); });
}
关于javascript - 我的函数读取条形码后如何单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29084718/