gpt4 book ai didi

javascript - 当我在一个页面中使用多个 Controller 时,Angular JS 只有一个 Controller 工作

转载 作者:搜寻专家 更新时间:2023-11-01 05:09:43 25 4
gpt4 key购买 nike

处理两个 Controller 的问题作为 Angular Js 的学习者,这就是我得到我的地方困惑

我用两个不同的应用程序和 Controller 和应用程序写了两个 据我所知,我们可以在一个页面中定义多个。

The first part div which is

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

worked well

enter image description here当我像这样使用两个 Controller 时

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<br><br>


<div ng-app="myApp1" ng-controller="personCtrl1">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>


<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
var app1 = angular.module('myApp1', []);
app1.controller('personCtrl1', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>

</body>
</html>

这就是问题所在

enter image description here

最佳答案

给你的第二个 div 一个 id 并添加这一行:

angular.bootstrap(document.getElementById("myApp1"), ['myApp1']);

http://jsfiddle.net/ADukg/9952/

关于javascript - 当我在一个页面中使用多个 Controller 时,Angular JS 只有一个 Controller 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111664/

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