gpt4 book ai didi

javascript - AngularJS 在 1 个应用程序中声明多个 Controller

转载 作者:行者123 更新时间:2023-12-03 07:35:26 25 4
gpt4 key购买 nike

我在 1 个应用程序中声明 2 个 Controller 时遇到问题。我的猜测是这是一个语法错误,但并不完全确定。

我收到的错误消息是“人员未定义”。

//这是我的应用程序文件

var myApp = angular.module('myApp',[]);


myApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});



myApp.controller('controller1', function($scope) {

var pets = [
{animal: "dog", years: "3 Years"},
{animal: "dog", years: "1 Years"},
];});


myApp.controller('controller2', function($scope) {


var people = [
{name: "john", bday: "september"},
{name: "nancy",bday: "december"},
];
});

在我的 html 页面上,我只是编写了一些 javascript 来尝试获取 people 数组的长度。

  var totalpeople = people.length;

注意:不确定这是否是重要信息,但我在 html 标记中声明了我的应用程序。

更新:这是我的 html 页面上的一些基本伪代码。

    <html ng-app="myApp">
<head>
<script>
var totalpeople = people.length;
</script>
</head>
<body>
<div ng-controler="controller1">
<table><tr ng-repeat="rows in pets">
<td>{[{pets.animal}]}</td><td>{[{pets.year}]}</td>
</tr>
</div>
<div ng-controler="controller2">
<table><tr ng-repeat="rows in people">
<td>{[{pets.name}]}</td><td>{[{pets.bday}]}</td>
</tr>
</div>
<div>
<table><tr>
<td><script>//not actually doing this, but totalpeople goes here </script></td>
</tr>
</div>

</body>

最佳答案

html 和脚本部分有很多错误。就像您将 ng-controller 拼错为 ng-controler 一样。而且,ng-repeat 逻辑是完全错误的。您缺少表标签等。我不知道为什么您想要插入 {{}} 来使用 {[{}]}

这是正在工作的插件:http://plnkr.co/edit/qs7mCrmElXGqGHdKAmNH?p=preview

    <html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="controller1">
<table><tr ng-repeat="rows in pets">
<td>{[{rows.animal}]}</td><td>{[{rows.years}]}</td>
</tr>
</table>
Total pets:{[{count}]}
</div>
<div ng-controller="controller2">
<table><tr ng-repeat="rows in people">
<td>{[{rows.name}]}</td><td>{[{rows.bday}]}</td>
</tr>
</table>
</div>
<br>
Total Pets:
<span id="show"></span>
<script>
var myApp = angular.module('myApp',[]);


myApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});



myApp.controller('controller1', function($scope) {

$scope.pets = [
{animal: "dog", years: "3 Years"},
{animal: "dog", years: "1 Years"}
];
$scope.count = $scope.pets.length;
document.getElementById('show').innerText=$scope.count;//use directive to achieve this,using jquery inside controller is bad practise. Used just for the purpose of demo.
});


myApp.controller('controller2', function($scope) {
$scope.people = [
{name: "john", bday: "september"},
{name: "nancy",bday: "december"},
];
});
</script>
</body>
</html>

关于javascript - AngularJS 在 1 个应用程序中声明多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630727/

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