【问题标题】:ng-model is not updated in ng-ifng-model 未在 ng-if 中更新
【发布时间】:2014-03-05 20:40:55
【问题描述】:

我无法在我的选择 html 块中检索我选择的选项 (ng-model=filter) 的实际值。

请注意,如果我没有设置 $scope.filter,那么即使我在下拉列表中选择了一个选项,$scope.filter 也会保持未定义。

我的 html 模板,filters.html:

<div ng-if="!showFilterTemplatePlz"> <button class="btn btn-primary" ng-click="showFilterTemplate()" type="button">Add Filter</button> </div> <div ng-if="showFilterTemplatePlz" class="inline"> <button class="btn btn-primary" ng-click="closeFilters()" type="button">Close filters</button> <label>filters</label> <select ng-model="filter" ng-options="filter.name for filter in filterOptions" class="form-control"> </select> <p>{{filter.name}}</p> <button ng-click="addFilter()" id="addFilterButton" class="btn btn-primary btn-sm btn-default" type="button"><i class="fa fa-plus"></i> Add</button> </div>

在我的指令中:

      .directive('filters', ['retrieveStaticDatasService','usersService', function(datasService, usersService) {
        return {
            restrict: 'E',
            scope: false,
            controller: function ($scope) {
                /* init */
                $scope.filterOptions = [
                    {name: "languages"},
                    {name: "nationality"},
                    {name: "location"}
                ];
                $scope.filter = $scope.filterOptions[0];
                /* end init */
                $scope.showFilterTemplate = function(){
                      $scope.showFilterTemplatePlz=true;
                }
                $scope.closeFilters = function(){
                    $scope.showFilterTemplatePlz=false;
                }
                $scope.addFilter = function(){
                    console.log("filter to add : " + $scope.filter.name);                        
                }
            },
            templateUrl: '/partials/components/filters.html'
        }
    }])

结果

我可以看到实际值出现在我的 html 中,但无论我选择什么选项,我都会在控制台中显示 $scope.filter.name 的“语言”!我截图给你看:http://hpics.li/4a37ae3

感谢您的帮助。

[编辑:我做了一些测试,并且我的模型只有在它们不在“&lt;div ng-if="..."&gt;&lt;/div&gt;”中时才会被设置和更新]。是不是不能直接在指令里面放一个ng-if?

回答Angularjs ng-model doesn't work inside ng-if

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    我找到了解决方案,问题是 ng-if 创建了一个子作用域。所以我在

    中将过滤器更改为 $parent.filter
     <select ng-model="$parent.filter" ng-options="option.name for option in filterOptions" class="form-control">
          </select>
    

    【讨论】:

    • “ng-if 创建了一个子作用域”这句话为我节省了几天的工作时间
    【解决方案2】:

    我认为您必须使用名为“过滤器”的对象来混淆它。您可能会覆盖 ng-options 对象而不是控制器对象。

    试试改成

    <select ng-model="filter" ng-options="option.name for option in filterOptions" class="form-control"></select>
    <p>{{filter.name}}</p>
    <button ng-click="addFilter()" id="addFilterButton" class="btn btn-primary btn-sm btn-default" type="button"><i class="fa fa-plus"></i> Add</button>
    

    【讨论】:

    • 我刚刚尝试过,但它仍然无法正常工作,我还尝试使用另一个名称而不是过滤器,但没有结果...我将编辑我的帖子,因为我没有说这是在指令中,并且也许问题来自那个事实。
    • 问题来自我的 ng-model 在 ng-if 中,但我不知道为什么。有人有想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多