gpt4 book ai didi

javascript - Angular : Two way data binding - directive with ng-repeat - not working

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:08 24 4
gpt4 key购买 nike

我坚持使用从指令到 Controller 的两种数据绑定(bind)方式。指令与 ng-repeat 一起使用。尝试了几件事,但无法在 Controller 中反射回指令范围变量。

angular.module("myApp", [])
.controller("Ctrl1", function ($scope) {
$scope.crossTabs = [1,2,3,4,5];
})
.directive("crossTab", function () {
return {
restrict: "E",
template: "<input type='text' ng-model='crossTab'/><button ng-click='changeLols()' id='clc'>change crossTabs</button>",
scope: {
crossTabs: '=',
crossTab: '='
},
controller: function($scope){
$scope.changeLols = function(){
// The below comment line is not working. It doesnt change anything. But the one uncommented which pushes an element updates the controllers 'crossTabs' variable as well.
//$scope.crossTabs = [3,4,5];
$scope.crossTabs.push(6);
}
},
link: function (scope, elem, attrs) {

}
}
});

HTML

<div ng-app="myApp" ng-controller="Ctrl1">
<div> Controller: {{ crossTabs }}</div><br>
<h5>Directive Section below</h5>
<cross-tab ng-repeat="crossTab in crossTabs" cross-tab="crossTab" cross-tabs="crossTabs"></cross-tab>
</div>

这是 jsfiddle:http://jsfiddle.net/keshav89/sezknp1t/1/

我错过了什么。我也尝试使用链接功能,但无济于事。

最佳答案

像这样把它们放在一个对象中:

$scope.viewModel = {
crossTabs: [1, 2, 3, 4, 5]
};

和:

<cross-tab ng-repeat="crossTab in viewModel.crossTabs" cross-tab="crossTab" cross-tabs="viewModel.crossTabs">

演示: http://jsfiddle.net/fzz9xabp/

可以找到关于原型(prototype)继承和 AngularJS 的一个很好的答案 here .

关于javascript - Angular : Two way data binding - directive with ng-repeat - not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27957558/

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