gpt4 book ai didi

javascript - AngularJS 组件之间的绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-03 00:47:19 24 4
gpt4 key购买 nike

我在 AngularJS 中确实遇到了绑定(bind)问题。我创建了 2 个组件(leftForm 和 rightForm),这些组件有一个输入和一个 p 元素。此 p 元素是一个计数,当您单击其他组件的输入按钮时,该计数会增加。

例如,我单击左按钮(左组件),然后右按钮(右组件)中的计数需要增加,我已经完成了这一点,但问题是我看不到这些变量 View ,所以我猜我在绑定(bind)上做错了什么。这是我的代码:

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>

最佳答案

要访问模板中的 $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>

关于javascript - AngularJS 组件之间的绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com