首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法访问模板指令中的任何路由

无法访问模板指令中的任何路由
EN

Stack Overflow用户
提问于 2018-06-18 14:06:51
回答 1查看 30关注 0票数 0

我有一个指令,我试图通过HTTP方法获取一些消息,基于一些选项

代码语言:javascript
复制
import publicListTpl from './public-list.html';

(() => {
    angular
        .module('news.crud.directives', [])
        .directive('newsPortalPublishedList', () => ({
            restrict: 'E',
            replace: true,
            templateUrl: publicListTpl,
            scope: {
                type: '@'
            },
            controller: ($scope, $http, FlashMessages) => {
                $http.get('/api/portal/'+$scope.type)
                    .then(response => $scope.news = response.data)
                    .catch(() => FlashMessages.add(FlashMessages.ERROR, 'La récupération des '+$scope.type+' a échoué.'));
            }
        })); })();

问题是,当我添加scope选项scope: {type: '@'}时,无法访问任何已定义的路由到我的publicListTpl模板中,例如url('news_edit', {id: news.id})将返回null

这是我的html模板:

代码语言:javascript
复制
<div class="portlet" ng-repeat="news in newsList">
    <div class="portlet light bordered">
        <div class="portlet-title">
            <div class="caption">
                {{ news.title }}
                <small>{{ news.subtitle }}</small>
            </div>
            <div class="actions">
                <a class="btn btn-info btn-sm" href="{{ url('news_edit', {id: news.id}) }}">
                    <i class="fa fa-pencil"></i> Editer
                </a>
            </div>
        </div>
        <div class="portlet-body">
            <img ng-if="news.image" src="{{ news.image }}" class="pull-left" height="100"/>
            <div ng-bind-html="news.shortContent"></div>
        </div>
    </div>
</div>

Ps:我甚至不知道源问题,所以你可以编辑这个问题,使它更清楚。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-18 14:28:55

我对您的指令做了一些更改,我使用了链接函数而不是控制器,我用news替换了ng-repeat中的item,因为用要遍历的数组的名称命名单个项并不是一个好的选择。

代码语言:javascript
复制
import publicListTpl from './public-list.html';
    
    (() => {
        angular
            .module('news.crud.directives', [])
            .directive('newsPortalPublishedList', ($http, FlashMessages) => ({
                restrict: 'E',
                replace: true,
                templateUrl: publicListTpl,
                scope: {
                    type: '@'
                },
                link: (scope) => {
                    $http.get('/api/portal/'+scope.type)
                        .then(response => scope.news = response.data)
                        .catch(() => FlashMessages.add(FlashMessages.ERROR, 'La récupération des '+scope.type+' a échoué.'));
                }
            })); })();
代码语言:javascript
复制
<div class="portlet" ng-repeat="item in news._embedded.items">
        <div class="portlet light bordered">
            <div class="portlet-title">
                <div class="caption">
                    {{ item.title }}
                    <small>{{ item.subtitle }}</small>
                </div>
                <div class="actions">
                    <a class="btn btn-info btn-sm" href="{{ url('news_edit', {id: item.id}) }}">
                        <i class="fa fa-pencil"></i> Editer
                    </a>
                </div>
            </div>
            <div class="portlet-body">
                <img ng-if="news.image" src="{{ item.image }}" class="pull-left" height="100"/>
                <div ng-bind-html="news.shortContent"></div>
            </div>
        </div>
    </div>

该指令的使用应是:

代码语言:javascript
复制
<news-portal-published-list type="url_goes_here"></news-portal-published-list>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50911329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档