绑定在组件之间不起作用 AngularJS
Binding not working between components AngularJS
我确实在 AngularJS 中遇到绑定问题。
我创建了 2 个组件(leftForm 和 rightForm),这些组件有一个输入和一个 p
元素。这个p
元素是一个计数,当你点击其他组件的输入按钮时它会增加。
例如我点击左按钮(左组件)然后右按钮(右组件)中的计数需要递增,我已经完成了这个,但问题是我看不到那些变量在视图中,所以我猜我在绑定方面做错了什么。这是我的代码:
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>
我确实在 AngularJS 中遇到绑定问题。
我创建了 2 个组件(leftForm 和 rightForm),这些组件有一个输入和一个 p
元素。这个p
元素是一个计数,当你点击其他组件的输入按钮时它会增加。
例如我点击左按钮(左组件)然后右按钮(右组件)中的计数需要递增,我已经完成了这个,但问题是我看不到那些变量在视图中,所以我猜我在绑定方面做错了什么。这是我的代码:
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>