2014-03-04 47 views
2

我没有在我的选择HTML集团取回我的选项(NG-模型=过滤器)的实际值。NG-模型更新不NG,如果

需要注意的是,如果我不设置$ 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

感谢您的帮助。

[编辑:我做了一些测试,我的模型设置好的和更新,只有当他们没有内部的“<div ng-if="..."></div>”。不允许将ng-if直接放入指令中吗?

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

回答

6

我找到了解决办法,问题是,NG-如果创建一个子范围。所以,我在

改变滤波器 $ parent.filter
<select ng-model="$parent.filter" ng-options="option.name for option in filterOptions" class="form-control"> 
     </select> 
0

我认为你是通过具有名为“过滤器”的对象混淆了。你可能会覆盖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> 
+0

我只是尝试,但它仍然没有工作,我也试图与另一个名称,而不是没有结果过滤器...我会编辑自己的帖子,因为我没有“T说,这是一个指令,也许问题来自于事实。 –

+0

问题来自于我的NG-模型里面NG-IF,但我不知道为什么。任何人有想法? –