gpt4 book ai didi

javascript - ng-click 在 ng-repeat 中使用 ng-include 绑定(bind)所有数据

转载 作者:行者123 更新时间:2023-11-30 14:46:36 25 4
gpt4 key购买 nike

我想借助 ng-click 将 html 页面包含在 ng-repeat 中。但是,它会加载所有 ng-repeat 元素的所有内容。我的要求是我只想绑定(bind)(ng-include)被点击的元素。
请找到附件供您引用。

HTML

<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="message in messages">
<a ng-click="clickMe()">Clike Me</a>
<span>{{ message.text }}---{{ message.type }}</span>
<div ng-include="templateUrl"></div>
</div>
</body>

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
var messages = [
{ text: "Standard Message", type: "success000" },
{ text: "Success Message!", type: "success111" },
{ text: "Alert Message!", type: "alert222" },
{ text: "secondary message...", type: "secondary333" }
]
$scope.name = 'shiva';
$scope.count = 0;
$scope.messages = messages;
$scope.clickMe = function () {
// alert('clicked');
$scope.count++;
$scope.templateUrl = "Page.html";
};
});



页面.Html

<b> Included html Code.{{message.type}}---count={{count}}

最佳答案

添加到 message 新属性 clicked。仅当 message.clickedtrue 时使用 ng-if 设置 ng-include。当您点击时,将message.clicked的状态更改为true

像这样的东西:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope){
var messages = [
{ text: "Standard Message", type: "success0" ,clicked:false},
{text:"Success Message!", type:"success00",clicked:false},
{text:"Alert Message!", type : "alert2",clicked:false},
{text:"secondary message...", type : "secondary3",clicked:false}
]
$scope.name = 'shiva';
$scope.count = 0;
$scope.messages = messages;
$scope.clickMe = function (message) {
// alert('clicked');
$scope.count++;
message.clicked=true;
$scope.templateUrl = "Page.html";
};

});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="message in messages">
<a ng-click="clickMe(message)">Clike Me</a>
<span>{{message.text}}---{{message.type}}</span>
<div ng-if="message.clicked" ng-include="templateUrl"></div>
</div>
</body>
</html>

关于javascript - ng-click 在 ng-repeat 中使用 ng-include 绑定(bind)所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48821264/

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