简体   繁体   English

AngularJs Modal从rootScope设置默认值

[英]AngularJs Modal set default value from rootScope

I have problem with angular modal value. 我对模态值有疑问。 I have $rootScope and on button im opening modal which contains , and I can't set default / selected value to that 我有$ rootScope和on即时消息包含包含的模式,我无法将默认值/选定值设置为

<form>
    <input type="button" class="mobileUpdate" ng-click="openMobileUpdateModal()" />

    <script type="text/ng-template" id="mobileModal.html">
         <form name="modalForm">
             <div class="modal-body">
                   <p>Country</p>
                   <select id="countryMobile" ng-model="countryMobile" class="select-styled" name="countryMobile" required>
                       <option ng-repeat="country in countries" value="{{country.id}}">{{country.name}}</option>
                   </select>
                </div>
             </div>
             <div class="modal-footer">
                 <input type="button" ng-click='confirm()' value="OK"/>
                <input type="button" ng-click='cancel()' value="Cancel"/>
             </div>
         </form>
     </script>

</form>


   $rootScope.openMobileUpdateModal = function () {

      $rootScope.countryMobile = $rootScope.country;
      var modalInstance = $modal.open({
        templateUrl: 'mobileModal.html',
        controller: ModalInstanceCtrl
      });
    };
    var ModalInstanceCtrl = function ($rootScope, $modalInstance) {
        $rootScope.confirm = function () {
         //do something
          $modalInstance.close();
        };
        $rootScope.cancel = function () {
          $modalInstance.dismiss();
        };
    };

Anyone know how to send value from $rootScope to modal, and set that value as default? 有谁知道如何将值从$ rootScope发送到模式,并将该值设置为默认值? Thanks 谢谢

First of all there is syntax error you have double quote twise in you select tag code. 首先存在语法错误,您在选择标记代码时使用双引号twise。 Update it as below then try. 如下更新它,然后尝试。 And use $root in your ng-model. 并在ng-model中使用$root

<select id="countryMobile" ng-model="$root.countryMobile" class="select-styled" name="countryMobile" required>
      <option ng-repeat="country in countries" value="{{country.id}}">{{country.name}}</option>
</select>

I'm not sure that I understand your problem correct but try do not use rootScope or modify and avoid rootScope modification. 我不确定我是否正确理解了您的问题,但请尝试不要使用rootScope或进行修改,并避免修改rootScope。

If you need use some value from rootScope: 如果需要使用rootScope的一些值:

  1. extract this value to local scope of particular controller and after that use it 将此值提取到特定控制器的本地范围,然后使用它
  2. Use WATCH operator to avoid situations when you render some value before it initialised. 使用WATCH运算符可以避免在初始化之前呈现某些值的情况。

<option ng-repeat> does not work in angularjs. <option ng-repeat>在angularjs中不起作用。 you should use 你应该使用

<select id="countryMobile" ng-model="countryMobile" class="select-styled"   name="countryMobile" required ng-options="country.id as country.name for country in contries"></select>

http://docs.angularjs.org/api/ng/directive/select http://docs.angularjs.org/api/ng/directive/select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM