ng模型的条件

时间:2016-11-05 16:23:13

标签: angularjs angular-ngmodel angular-ng-if

请用ng-model帮助我。我有一个输入字段,用于从数组中选择替换值'海报'。过滤器选择的Yhe值显示在列表中。默认情况下会显示该列表' grund tour'。当我以不在数组中的值的形式输入时 - 在列表中显示一个空字段,但我需要显示或者数组的值或默认值(如果输入的表单值不是的数组)。我不知道该怎么做。 我认为可以申请ng-model的条件,使其仅适用于数组的值

我的代码html:

<div class="container" ng-controller = "FormController">
    <div class="container__item container__form"><!-- 1 -->
        <form action="" class="form">
            <input type="text" class="form__input" placeholder="Search..." autocomplete="on" list="posters" ng-model="posterTitle"  >
            <datalist id="posters" >
                <option ng-repeat="poster in posters" >{{poster.title}}
                </option>
            </datalist> 
        </form>         
    </div>
    <div class="container__item container__list"><!-- 2 --> 
        <ul class="list" id="item">
            <li class="list__item" ng-if="poster.title =='The grand tour'" ng-repeat="poster in posters | filter:posterTitle"><i class="fa fa-rocket list__fa" aria-hidden="true"></i>{{poster.title}}</li>
            <li class="list__item" ng-if="poster.title!='The grand tour'" ng-repeat="poster in posters | filter:{title:posterTitle}">{{poster.title}}</li>
        </ul>
    </div>      
</div>

控制器:

'use strict';
  angular.module('oldmenTest')
   .controller('FormController', ['$scope', 'postersName', function($scope, postersName) {
    $scope.posters= postersName.getPosters();

    $scope.posterTitle = '';
 }]);

Var:

'use strict';

angular.module('oldmenTest')
 .service('postersName', function() {

var posters = [{
    title: 'var1',
    description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    image: 'resources/images/mars.jpg'
}, {
    title: 'var2',
    description: 'TLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    image: 'resources/images/earth.jpg'
}, {
    title: 'var3',
    description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    image: 'resources/images/venus.jpg'
}
];

this.getPosters = function(){
    return posters;
};

});

感谢你的支持!

1 个答案:

答案 0 :(得分:0)

据我了解,您必须根据标题在特定属性或所有属性上有条件地应用过滤器。

您可以执行以下操作。

from django.utils import feedgenerator
class Rss201rev2FeedUnescaped(feedgenerator.Rss201rev2Feed):
    """
    Rss 2.01 Feed that doesn't escape content
    """
    def write(self, outfile, encoding):
        """
        code is mainly copy-paste from django.utils.feedgenerator.Rss201rev2Feed
        except that the handler is set to UnescapedXMLGenerator
        """
        handler = UnescapedXMLGenerator(outfile, encoding)
        handler.startDocument()
        handler.startElement("rss", self.rss_attributes())
        handler.startElement("channel", self.root_attributes())
        self.add_root_elements(handler)
        self.write_items(handler)
        self.endChannelElement(handler)
        handler.endElement("rss")