[英]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.