gpt4 book ai didi

angularjs - 指令未使用 controllerAs 和 bindToController 使用异步数据更新 View

转载 作者:行者123 更新时间:2023-12-05 01:22:41 24 4
gpt4 key购买 nike

我在获取更新 View 的指令时遇到了一些问题。

在我的 Controller 中,我为 <tiki-list> 的属性设置了初始值。指示。然后,2 秒后,我正在更新 vm.listObjectSelected测试其异步行为。

但是, View 并未反射(reflect)更新。

Controller :

    var listObject = [{"display":"display1", "value":"value1"}, {"display":"display2", "value":"value2"}, {"display":"display3", "value":"value3"}]

vm.listObject = listObject
vm.listObjectSelected = [{"display":"display1", "value":"value1"}]

$timeout(function(){

vm.listObjectSelected = [{"display":"display1", "value":"value1"}, {"display":"display3", "value":"value3"}]

}, 2000)

HTML

<tiki-list max="" type="multi" list="editController.listObject" selected="editController.listObjectSelected"></tiki-list>

指令

(function(){

'use strict';

angular.module("tiki").directive("tikiList", tikiList)

function tikiList(helper){

var directive = {

restrict:"EA",
scope:{

list: "=", //the object to repeat over, this contains 2 array's
retunObject: "=", //the array that is outputted
selected: "=", //preselected values
max: "=", //maximum range, other elements are greyed out, starts at 0
title:"@title", //the title of this list
type:"@type", //[single, multi]

},
templateUrl:"js/directive/list.html",
link:link,
bindToController: true,
controllerAs:"vm",
controller:controller

}

return directive

function link(scope, el, attr, ctrl){

scope.vm.onClick = onClick

// preprocess the "list" if there is a "selected" attribute
// the "selected" attribute is an object that contains the selected items
// return a "selectedItems" array containing the indeces of matching display names
// add the .active property to the "list" object to the correct indeces

if(scope.vm.selected){

var selectedItems = helper.isItemInList(helper.createArrayFromProperty(scope.vm.selected, "display"), helper.createArrayFromProperty(scope.vm.list, "display"))

for(var i = 0; i < selectedItems.length; i++){

scope.vm.list[selectedItems[i]].active = true

}

}

// add the property .disabled to the "list" if there is a max attribute
// the property will be added to all indeces that exceed the max value

if(scope.vm.max){

for(var y = 0; y < scope.vm.list.length; y++){

if(y >= scope.vm.max){

scope.vm.list[y].disabled = true

}

}

}

function onClick(index){

// only allow items that are in range of the "max" attribute are allowed to be clicked

if(!scope.vm.max || index < scope.vm.max){

if(scope.vm.type === "single"){

angular.forEach(scope.vm.list, function(val, key){

scope.vm.list[key].active = false

})

scope.vm.list[index].active = true

}

if(scope.vm.type === "multi"){

scope.vm.list[index].active = !scope.vm.list[index].active

}

}

}

scope.vm.listing = scope.vm.list

}


}

controller.$inject = [];

function controller(){




}


})()

指令模板

  <ul class="listOptions">
<li class="listOptions-title" ng-class="{'show':title}">{{vm.title}}</li>
<li ng-click="vm.onClick($index)" ng-class="{'active':list.active, 'disabled':list.disabled}" ng-repeat="list in vm.listing track by $index">{{list.display}}</li>
</ul>

我认为它与 controllerAs 有关,但我无法理解它。

谢谢,

最佳答案

我认为原因是 Array 是引用类型。当您在服务或异步步骤中更改数据时,数据点会转到内存中的新位置,但指令或 Controller 中的数据不会更改。

而不是像这样编写你的函数:

$timeout(function(){

vm.listObjectSelected = [{"display":"display1", "value":"value1"}, {"display":"display3", "value":"value3"}]

}, 2000)

你应该尝试这样做:

$(timeout(function(){vm.listObjectSelected.push({you need data here})},200)

或者你可以使用一个 promise ,你可以返回一个 promise 并在指令中获取它,使用

promise.then(function(){//let data = service.data again}

希望对您有所帮助。

关于angularjs - 指令未使用 controllerAs 和 bindToController 使用异步数据更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30303714/

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