NG-INIT时NG-选项列表改变不工作 [英] ng-init not working when the ng-options list is changed

查看:152
本文介绍了NG-INIT时NG-选项列表改变不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面选择要素负荷,并选择在控制负载的第一个选项。
更改基于另一个下拉列表中positionAllowedList。
但是NG-init不会选择第一项。

 <选择NG-的init =col.positionselected = col.positionAllowedList [0] .value的NG选项=pos.value作为pos.text在山坳POS。 positionAllowedListNG模型=col.positionselected>
                                                < /选择>$ scope.ChangeBookingIso =功能(COL){
                $。每个(col.bookingIsoList,功能(I,项目){
                        .... someLogic像以前一样改变它完全相同的结构的JSON列表中第一次加载.....
                        col.positionAllowedList = positionAllowedList;                })            }


解决方案

您正在使用您的使用了错误的指令。 NG-INIT 从未被设计来初始化基于在视图任意的(虽然可能被滥用)前pressions。 NG-INIT 文档本身说:


  

的唯一适当使用ngInit的是混叠ngRepeat的特殊性能,如在下面的演示看到。除了这种情况下,你应该使用控制器,而不是ngInit在一个范围内初始化值。


所以一般用法是NG重复的别名,比如 $指数 $第一个等其特殊的属性,以便例如,当您使用嵌套的 NG-重复子内的NG-重复,您可以访问父NG-repeat的 $指数通过别名由设置NG-INIT 不使用 $父。$指数 $父。 $父... $指数等。

这只是一次初始化为好,如果你看的来源NG-INIT 是pretty简单和直接的。

  VAR ngInitDirective = ngDirective({
  优先级:450,
  编译:功能(){
    返回{
      pre:功能(范围,元素,ATTRS){
        。范围$的eval(attrs.ngInit);
      }
    };
  }
});

如果您发现没有序已经除权$每消化周期评估再p $ pssion创建。的那么勿庸置疑当你打算它的工作这是行不通的。

所以只要删除 NG-INIT 选择键,而是设置属性山坳。 positionselected 内的控制器本身。

The below select element loads and selects the first option on load of the control . I change the positionAllowedList based on another dropdown. But ng-init does not select the first item.

<select ng-init="col.positionselected = col.positionAllowedList[0].value" ng-options="pos.value as pos.text for pos in col.positionAllowedList" ng-model="col.positionselected">
                                                </select>

$scope.ChangeBookingIso = function (col) {
                $.each(col.bookingIsoList, function (i, item) {                   
                        ....someLogic to change it is exactly the same structure the json list as before at the first time loaded.....
                        col.positionAllowedList = positionAllowedList;

                })

            }

解决方案

You are using the wrong directive for your usage. ng-init was never designed to initialize based on arbitrary(though can be misused) expressions on the view. ng-init documentation itself says:

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

So general usage is with ng-repeat for aliasing its special properties like $index, $first etc so for example when you use nested ng-repeat within the child ng-repeat you can access parent ng-repeat's $index via alias set by ng-init without using $parent.$index, $parent.$parent...$index etc.

It is only one time initialization as well, if you look at the source for ng-init it is pretty simple and straight forward.

var ngInitDirective = ngDirective({
  priority: 450,
  compile: function() {
    return {
      pre: function(scope, element, attrs) {
        scope.$eval(attrs.ngInit);
      }
    };
  }
});

If you notice there is no watch created inorder to have the expression re-evaluated during every digest cycle. So unsurprisingly it does not work as you intended it to work.

So just remove ng-init from select and instead set the property col.positionselected within the controller itself.

这篇关于NG-INIT时NG-选项列表改变不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆