简体   繁体   English

没有data-ng-bind无法打印var

[英]Can't print var without data-ng-bind

I'm trying to print vars inside {{ }} but it doesn't working. 我正在尝试在{{ }}打印vars,但是它不起作用。 When I use data-ng-bind it works perfectly but I need to print some vars inside HTML tags. 当我使用data-ng-bind它可以很好地工作,但是我需要在HTML标签内打印一些变量。

Here's my code: 这是我的代码:

HTML: HTML:

<aside class="main-sidebar" data-ng-controller="MenuController as menu" ng-show="menu.user.authenticated">
<section class="sidebar">
    <ul class="sidebar-menu" ng-repeat="category in items.sidebar">
        <li class="header" ng-show="!category.needAdmin || menu.user.isAdmin == category.needAdmin">{{category.name}}</li>
        <li ng-class="{'treeview': element.elements.length > 0}" ng-repeat="element in category.elements" ng-if="category.elements.length > 0">
            <a href="#">
                <i class="fa fa-{{element.fa-icon}}"></i> {{element.title}} <i class="fa fa-angle-left pull-right"></i>
            </a>
            <ul class="treeview-menu" ng-if="element.elements.length > 0">
                <li ng-repeat="subelement in element.elements" ui-route="/{{subelement.link}}" ng-class="{active: $uiRoute}">
                    <a mean-token="subelement.link" ui-sref="{{subelement.link}}"><i class="fa fa-circle-o"></i> {{subelement.title.length}}</a>
                </li>
            </ul>
        </li>
    </ul>
</section>

JS: JS:

'use strict';

angular.module('mean.system').controller('MenuController', ['$scope', '$rootScope', 'MeanUser', '$state',
function($scope, $rootScope, MeanUser, $state) {

    var vm = this;
    vm.user = {
        authenticated: MeanUser.loggedin,
        user: MeanUser.user,
        isAdmin: MeanUser.isAdmin
    };

    $scope.isCollapsed = false;

    $rootScope.$on('loggedin', function() {
        vm.user = {
            authenticated: MeanUser.loggedin,
            user: MeanUser.user,
            isAdmin: MeanUser.isAdmin
        };
    });

    vm.logout = function(){
        MeanUser.logout();
    };

    $rootScope.$on('logout', function() {
        vm.user = {
            authenticated: false,
            user: {},
            isAdmin: false
        };
        $state.go('auth.login');
    });

    $scope.items = {
        'top': [
            {
                'title': '',
                'link': '',
                'needAdmin': false,
                'elements': [
                    {
                        'title': '',
                        'link': ''
                    }
                ]
            }
        ],
        'sidebar': [
            {
                'name': 'DOCUMENTACIÓN',
                'needAdmin': true,
                'elements': [
                    {
                        'title': 'Artículos',
                        'link': '',
                        'fa-icon': 'check',
                        'elements': [
                            {
                                'title': 'Ver Artículos',
                                'link': 'all articles'
                            },
                            {
                                'title': 'Crear Artículo',
                                'link': 'create article'
                            }
                        ]
                    }
                ]
            }
        ]
    };
}
]);

I tried using $rootScope instead of $scope but still not work 我尝试使用$ rootScope而不是$ scope,但仍然无法正常工作

If your data-ng-bind is working fine, it's recommended to use it. 如果您的data-ng-bind工作正常,建议使用它。 For some reason ( explain in details in this question) it's better to use ng-bind than {{ }} . 出于某种原因(在问题中进行详细说明),使用ng-bind{{ }}更好。 Briefly it's all about the refresh/loading of your page. 简而言之,这全部与刷新/加载页面有关。 ng-bind avoid to see something before the data are set meawhile {{ }} will be show even if the data isn't set. ng-bind避免在设置数据之前先查看某些内容,即使未设置数据也将显示{{ }}

If you need to print the var in your HTML you can simply use span . 如果您需要在HTML中打印var,则只需使用span It will be something like this : 将会是这样的:

 <span data-ng-bind="element.title"></span>

Edit : 编辑:

As regards the class, one of the way of doing it, is a function if you have to set the name dynamically. 关于类,一种实现方法是,如果必须动态设置名称,则该函数是一个函数。 For example this : 例如:

$scope.appliedClass = function(element) {
        return "fa-"+element.fa-icon; 
}

And then in your HTML : 然后在您的HTML中:

<i class="fa" ng-class="menu.appliedClass(element)"></i>

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

相关问题 来自$ scope控制器的data-ng-bind - data-ng-bind from $scope controller Angular JS-{{}}不起作用,但是data-ng-bind显示范围 - Angular JS - {{ }} not working but data-ng-bind display the scope data-ng-bind不适用于 <option>元件 - data-ng-bind does not work with <option> element ng-bind或表达式不会打印 - ng-bind or expression doesn't print Change href无法更新绑定到ng-src或ng-href的数据 - Change href can't update the data which bind to ng-src or ng-href 具有Angular 4的ng2-chart:无法绑定到“数据”,因为它不是“ canvas”的已知属性 - ng2-chart with Angular 4: Can't bind to 'data' since it isn't a known property of 'canvas' 在AngularJS中,我是否只在视图中使用ng-model和ng-bind而不涉及使用控制器? - In AngularJS can I use the ng-model and the ng-bind only in the view without involving the use of a controller? 是否可以在不使用 ng-model 和 ng-bind 的情况下实现 angular 的双向数据绑定? - Is it possible to achieve two way data binding in angular without using ng-model and ng-bind? 无法绑定数据以查看angularjs - Can't bind data to view angularjs 为什么数据无法与JSON文件绑定? - Why the data can't be bind with JSON file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM