我确实对AngularJS中的绑定有问题。
我创建了2个组件(leftForm和rightForm),这些组件具有一个输入和一个p
元素。 p
元素是一个计数数字,当您单击其他组件的输入按钮时,计数会增加。
例如,我单击leftButton(左侧组件),然后需要增加右侧按钮(右侧组件)中的计数,我已经完成了此操作,但是问题是我无法在视图,所以我猜我在绑定中做错了什么。这是我的代码:
angular.module('testApp', [])
.controller('Controller', function ($rootScope) {
var vm = this;
$rootScope.valueLeft = 0;
$rootScope.valueRight = 0;
vm.rightButton = function () {
$rootScope.valueLeft += 1;
}
vm.leftButton = function () {
$rootScope.valueRight += 1;
}
})
.component('leftForm', {
templateUrl: 'leftForm.html',
controller: 'Controller',
controllerAs: 'vm',
bindings: {
onUpdate: '&'
}
})
.component('rightForm', {
templateUrl: 'rightForm.html',
controller: 'Controller',
controllerAs: 'vm',
bindings: {
onUpdate: '&'
}
});
<!doctype html>
<html ng-app="testApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
crossorigin="anonymous">
</head>
<body>
<div ng-controller="Controller as vm">
<div class="container" style="text-align: center; padding-top: 2%;">
<div class="row" style="padding: 2%; border: 1px solid;">
<div class="col">
<left-form></left-form>
</div>
<div class="col"></div>
<div class="col">
<right-form></right-form>
</div>
</div>
</div>
</div>
</body>
<script type="text/ng-template" id="leftForm.html">
<p>{{$rootScope.valueLeft}}</p>
<button ng-click="vm.leftButton()">+1</button>
</script>
<script type="text/ng-template" id="rightForm.html">
<p>{{$rootScope.valueRight}}</p>
<button ng-click="vm.rightButton()">+1</button>
</script>
</html>
答案 0 :(得分:1)
要访问模板中的$rootScope
变量,您只能将use $root
as a reference to the $rootScope
设为{{$root.valueLeft}}
和{{$root.valueRight}}
。
这是一个工作示例:
angular.module('testApp', [])
.controller('Controller', function ($rootScope) {
var vm = this;
$rootScope.valueLeft = 0;
$rootScope.valueRight = 0;
vm.rightButton = function () {
$rootScope.valueLeft += 1;
}
vm.leftButton = function () {
$rootScope.valueRight += 1;
}
})
.component('leftForm', {
templateUrl: 'leftForm.html',
controller: 'Controller',
controllerAs: 'vm',
bindings: {
onUpdate: '&'
}
})
.component('rightForm', {
templateUrl: 'rightForm.html',
controller: 'Controller',
controllerAs: 'vm',
bindings: {
onUpdate: '&'
}
});
<html ng-app="testApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
crossorigin="anonymous">
</head>
<body>
<div ng-controller="Controller as vm">
<div class="container" style="text-align: center; padding-top: 2%;">
<div class="row" style="padding: 2%; border: 1px solid;">
<div class="col">
<left-form></left-form>
</div>
<div class="col"></div>
<div class="col">
<right-form></right-form>
</div>
</div>
</div>
</div>
</body>
<script type="text/ng-template" id="leftForm.html">
<p>{{$root.valueLeft}}</p>
<button ng-click="vm.leftButton()">+1</button>
</script>
<script type="text/ng-template" id="rightForm.html">
<p>{{$root.valueRight}}</p>
<button ng-click="vm.rightButton()">+1</button>
</script>
</html>