如何在angularjs中访问嵌套控制器中的ng模型
我正在与一个父控制器和几个并行的子控制器一起工作 路由器 page.html 在按钮的发送功能中,第一个模型工作正常,但我无法访问第二个模型,它总是返回未定义的结果。还有别的办法吗 提前感谢您看到vm.matricula对象未定义的原因是您的按钮定义在vm控制的范围之外-按钮的sendpedido,vm.matricula方法不知道vm是什么 如果要在vm控制的div中包含该按钮,那么vm.matricula应该很好。我在这里没有这样做,但我建议在开始嵌套每个控制器时使用ctrl作为语法-这会使事情更清楚如何在angularjs中访问嵌套控制器中的ng模型,angularjs,controller,nested,Angularjs,Controller,Nested,我正在与一个父控制器和几个并行的子控制器一起工作 路由器 page.html 在按钮的发送功能中,第一个模型工作正常,但我无法访问第二个模型,它总是返回未定义的结果。还有别的办法吗 提前感谢您看到vm.matricula对象未定义的原因是您的按钮定义在vm控制的范围之外-按钮的sendpedido,vm.matricula方法不知道vm是什么 如果要在vm控制的div中包含该按钮,那么vm.matricula应该很好。我在这里没有这样做,但我建议在开始嵌套每个控制器时使用ctrl作为语法-这会使
<!-- pageCtrl scope -->
<!-- start of vm scope -->
<div class="item item-input" ng-controller="matriculaCtrl as vm">
<label class="item-input-wrapper">
<span class="input-label">Matricula</span>
<input type="text" placeholder="" ng-model="vm.matricula">
</label>
<button class="button button-small button-positive"
ng-click="vm.scan()">
<i class="icon ion-qr-scanner"></i>
</button>
<!-- button is now inside the vm scope -->
<button class="button button-block button-positive icon-right ion-chevron-right"
ng-click="send(pedido, vm.matricula)">
Enviar
</button>
</div>
<!-- end vm scope -->
我认为第二个控制器“matriculaCtrl”应该转换为一个服务。您可以参考此内容以了解更多信息
<label class="item item-input">
<span class="input-label">Pedido</span>
<input type="number" ng-model="pedido">
</label>
<div class="item item-input" ng-controller="matriculaCtrl as vm">
<label class="item-input-wrapper">
<span class="input-label">Matricula</span>
<input type="text" placeholder="" ng-model="vm.matricula">
</label>
<button class="button button-small button-positive" ng-click="vm.scan()">
<i class="icon ion-qr-scanner"></i>
</button>
</div>
<!--more controllers-->
<button
class="button button-block button-positive icon-right ion-chevron-right"
ng-click="send(pedido, vm.matricula)">
Enviar
</button>
.controller('pageCtrl', ['$scope', '$stateParams', 'CustomerService', function ($scope, $stateParams, CustomerService) {
$scope.send = function(pedido, matricula){
console.log(pedido+'-'+matricula);
}
}])
.controller('matriculaCtrl', function ($scope, $rootScope, $cordovaBarcodeScanner, $ionicPlatform) {
var vm = this;
vm.scan = function () {
$ionicPlatform.ready(function () {
$cordovaBarcodeScanner
.scan()
.then(function (result) {
vm.matricula = result.text;
}, function (error) {
vm.matricula = 'Error: ' + error;
});
});
};
vm.matricula = '';
})
<!-- pageCtrl scope -->
<!-- start of vm scope -->
<div class="item item-input" ng-controller="matriculaCtrl as vm">
<label class="item-input-wrapper">
<span class="input-label">Matricula</span>
<input type="text" placeholder="" ng-model="vm.matricula">
</label>
<button class="button button-small button-positive"
ng-click="vm.scan()">
<i class="icon ion-qr-scanner"></i>
</button>
<!-- button is now inside the vm scope -->
<button class="button button-block button-positive icon-right ion-chevron-right"
ng-click="send(pedido, vm.matricula)">
Enviar
</button>
</div>
<!-- end vm scope -->