这里的jsfiddle: https://jsfiddle.net/vikramkute/eq3zpmp9/5/
我新的角度。我有一个对象需要在HTML中添加运行时。我使用的角度1.2.25
预期输出是
1 Quest
2 Quest
3 Quest
,但我得到最后的值重复三次。根据我的试验和错误,我觉得问题在于编译。我尝试了在不同论坛上提供的不同解决方案,但都没有成功任何帮助非常感谢。谢谢。
在指令(链接功能内)
scope.obj =
[
{
"questionText": "1 Quest"
},
{
"questionText": "2 Quest"
},
{
"questionText": "3 Quest"
}
]
scope.addData = function() {
for (var i = 0; i < scope.obj.length; i++) {
addSlide(scope.obj[i]);
}
}
addSlide = function (obj) {
scope.singleObj = obj;
el = $('<div ng-bind="singleObj.questionText"></div>');
scope.owl.append(el);
$compile(el)(scope);
};
输出:
3 Quest
3 Quest
3 Quest
这里充满指令:
angular.module('journeycarousel', [])
.directive('journeyCarousel', function ($compile) {
return {
restrict: 'E',
templateUrl: '../components/journeyCarousel/journeyCarousel.html',
transclude: true,
link: function (scope, element) {
scope.obj =
[
{
"questionText": "1 Quest"
},
{
"questionText": "2 Quest"
},
{
"questionText": "3 Quest"
}
]
scope.addData = function() {
for (var i = 0; i < scope.obj.length; i++) {
addSlide(scope.obj[i]);
}
}
addSlide = function (obj) {
scope.singleObj = obj;
el = $('<div ng-bind="singleObj.questionText"></div>');
scope.owl.append(el);
$compile(el)(scope);
};
}
}
});
上面的代码是简化版本。这是实际代码:
scope.singleObj = obj;
el = $('<div class="questionContainer" <div ng-repeat="obj in singleObj"> ng-click="onSlideClick($event,singleObj)"> <div class="item"> <div class="questionSection" ng-bind="singleObj.questionText"></div> <div class="answerSection" ng-bind="singleObj.questionAnswer + singleObj.questionUnitOfMeasure"></div> </div> </div>');
$('.owl-carousel').owlCarousel('add', el).owlCarousel('update');
$compile(el)(scope);
显示指令的完整代码。 –
在你的指令中使用'terminal:true' –
pro.mean我试过terminal:true。但没有运气。如果我添加$('。owl-carousel')。append(scope.singleObj.questionText);那么我获得3个不同的价值。但不知何故$编译是最后一个。我试图调试,发现第一次调用带有第一个值。第二次呼叫来自第二个值,但更新第一和第二位,第三次呼叫将更新所有3个值。 – TechFreak