【发布时间】:2016-06-06 14:07:01
【问题描述】:
我正在使用gulp 开发angular 应用程序。
如果我使用gulp serve 运行我的应用程序,我可以从外部访问我的scope,我可以在 chrome 控制台中成功运行此代码
angular.element("html[ng-app='admin']").scope().myVariable
但如果我运行 gulp build 并将 dist 文件夹部署到我的 Web 服务器,我将无法获取控制器范围变量。
怎么可能?
有我的gulp(默认来自 yeoman 生成器)
gulp.task('html', ['inject', 'partials'], function () {
var partialsInjectFile = gulp.src(path.join(conf.paths.tmp, '/partials/templateCacheHtml.js'), { read: false });
var partialsInjectOptions = {
starttag: '<!-- inject:partials -->',
ignorePath: path.join(conf.paths.tmp, '/partials'),
addRootSlash: false
};
var htmlFilter = $.filter('*.html');
var jsFilter = $.filter('**/*.js');
var cssFilter = $.filter('**/*.css');
var assets;
return gulp.src(path.join(conf.paths.tmp, '/serve/*.html'))
.pipe($.inject(partialsInjectFile, partialsInjectOptions))
.pipe(assets = $.useref.assets())
.pipe($.rev())
.pipe(jsFilter)
.pipe($.ngAnnotate())
// .pipe($.uglify({ preserveComments: $.uglifySaveLicense })).on('error', conf.errorHandler('Uglify'))
.pipe(jsFilter.restore())
.pipe(cssFilter)
.pipe($.replace('../../bower_components/bootstrap-sass-official/assets/fonts/bootstrap/', '../fonts/'))
.pipe($.csso())
.pipe(cssFilter.restore())
.pipe(assets.restore())
.pipe($.useref())
.pipe($.revReplace())
.pipe(htmlFilter)
//.pipe($.minifyHtml({
// empty: true,
// spare: true,
// quotes: true,
// conditionals: true
//}))
.pipe(htmlFilter.restore())
.pipe(gulp.dest(path.join(conf.paths.dist, '/')))
.pipe($.size({ title: path.join(conf.paths.dist, '/'), showFiles: true }));
});
编辑: 有评价结果
angular.element("html[ng-app='admin']").scope()
处于开发模式(gulp serve)
$$ChildScope: null
$$childHead: null
$$childTail: null
$$listenerCount: Object
$$listeners: Object
$$nextSibling: null
$$prevSibling: null
$$watchers: null
$$watchersCount: 0
$id: 3
$parent: Scope
也有相同的,但在生产中(gulp build)
$$ChildScope: ChildScope()
$$childHead: ChildScope
$$childTail: ChildScope
$$listenerCount: Object
$$listeners: Object
$$nextSibling: null
$$prevSibling: null
$$watchers: Array[3]
$$watchersCount: 22
$id: 3
$parent: Scope
main: Object
setData: (data)
我对 main 对象(控制器变量)很感兴趣。
编辑2:
我也尝试了angular.reloadWithDebugInfo();,但结果相同。
【问题讨论】:
-
I am not able to get controllers scope variable- 当你尝试时会发生什么? -
我编辑了我原来的问题。我还调查了父、根范围,但我找不到属性 main。