gpt4 book ai didi

javascript - 本地存储数据未进入 Angularjs 中的头文件

转载 作者:行者123 更新时间:2023-12-03 05:18:24 27 4
gpt4 key购买 nike

我正在使用 Mean Stack 并使用 AngularJs 和 NodeJs。我用过

本地存储数据并在主要内容中获取数据工作正常,但这

数据未进入 header 我正在使用包含 header 的主文件index.ejs

<div data-ng-include="'js/view/header.html'"  class="page-header navbar navbar-fixed-top">
</div>
<!-- END HEADER -->

<div class="clearfix">
</div>

<!-- BEGIN CONTAINER -->
<div class="container">
<div class="page-container">
<!-- BEGIN SIDEBAR -->
<div ng-include="'js/view/sidebar.html'" class="page-sidebar-wrapper">
</div>
<!-- END SIDEBAR -->
<div class="page-content-wrapper">
<div class="page-content">

<div ng-view></div>

<div ui-view class="fade-in-up">
</div>
<!-- END ACTUAL CONTENT -->
</div>
</div>
</div>
<!-- BEGIN FOOTER -->
<div data-ng-include="'js/view/footer.html'" class="page-footer">
</div>
<!-- END FOOTER -->
</div>

这是使用 localStorage 的函数

  app.controller("usercontroller",function($scope,$http, $localStorage,$location,flash){

$scope.registeruser = $localStorage.userData;
$scope.adminData = $localStorage.adminData;
$scope.alluser = $localStorage.alluser
$scope.flash = flash;
$scope.message = "New user added successfully";



/****************************************************Admin Login**********************************/



$scope.loginpage = function(){

// console.log("hello world");
$http({
method: 'POST',
url: '/api/login',
data: {email:$scope.email, password:$scope.password}
}).then(function successCallback(response) {
if(response.data.error)
{
alert("Invalid email pasword");
}
else
{

$scope.dp = response.data;
$localStorage.adminData = $scope.dp;
console.log($localStorage.adminData);
$location.path('/dashboard');

}
}, function errorCallback(response) {
alert("Invalid email pasword");
});
}

});

这是我的路由文件app.js

var express = require('express');
var path = require('path');
var multer = require('multer');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var api = require('./routes/api');
var users = require('./routes/users');

/* Database connectivity here */
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/ditroapp');
var db = mongoose.connection;

db.on('error', function (err) {
console.log('mongo connection error', err);
});

db.once('open', function () {
console.log('mongo connected.');
});

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/api', api);
app.use('/users', users);
//app.use('/login', login);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});


// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;

当我得到{{adminData.name}}时进入 header.html 它没有显示。

但是如果我在使用的主内容文件中使用相同的内容

<div ng-view></div>这显示了管理员的姓名。

为什么它在头文件中不起作用。

最佳答案

在 app.js 中尝试一下

app.run(function($rootScope,$localStorage) {

$rootScope.admindata= $localStorage.adminData;
});

关于javascript - 本地存储数据未进入 Angularjs 中的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506224/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com