2016-02-24 64 views
1

我使用gulp來開發angular應用程序。
如果我運行使用gulp serve我的應用程序,我可以訪問我的scope從外面,我可以在Chrome控制檯成功無法訪問角度範圍外變量後,從吞吐量構建

angular.element("html[ng-app='admin']").scope().myVariable

運行此代碼,但如果我運行gulp builddist文件部署到我的Web服務器,我無法獲取控制器範圍變量。
這怎麼可能?

還有就是我gulp(從自耕農發電機默認)

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()

評價結果(一整套服務)

$$ChildScope: null 
$$childHead: null 
$$childTail: null 
$$listenerCount: Object 
$$listeners: Object 
$$nextSibling: null 
$$prevSibling: null 
$$watchers: null 
$$watchersCount: 0 
$id: 3 
$parent: Scope 

A第二有相同的,但在生產中(大口構建)

$$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) 

蔭在對象(控制器變量)有趣。

EDIT2: 我也試過angular.reloadWithDebugInfo();,但結果相同。

+0

'我無法獲取控制器範圍變量 - 當您嘗試時會發生什麼? – BroiSatse

+0

我編輯了我原來的問題。我也調查了父母,根範圍,但Iam無法找到屬性** main **。 – teomartin

回答

0

你或者其中一個插件已經禁用了它嗎?

.config(['$compileProvider', function($compileProvider) { 
    $compileProvider.debugInfoEnabled(false); 
}]) 

檢查:Disabling Debug Data in AngularJS application 如果是這樣的問題,在控制檯angular.reloadWithDebugInfo();運行應該有所幫助。

另一件要檢查的事情 - 如果你正在使用javascript代碼縮小,那麼可能是某些依賴注入語句被錯過了。我的意思是聲明應該包括正確的DI項命名,如下所示:

var controllerFunction = function($scope, $stateParams, $rootScope) { 
    //do stuff 
} 

angular.module('myApp.myModule') 
.controller('controllerName', ['$scope', '$stateParams', '$rootScope', controllerFunction]); 
+0

我已正確設置'$ compileProvider.debugInfoEnabled(真);'如果我禁用此功能蔭甚至不能夠通過調用獲得範圍。我undefind得到的,而不是空範圍:'angular.element(「HTML [NG-應用= 「管理員」]「)。範圍()' – teomartin

+0

檢查更新的答案 – shershen

+0

我一飲而盡(見我原來的問題註釋行)關閉mimification和‘醜化’。 Iam也使用ngInject來DI我的依賴關係。我注入我的依賴關係如下:'角度 .module( 'xx.module') 。工廠('MyService',myService); /* @ngInject */ 函數myService($ resource,coreConfig){' – teomartin

0

問題出在我的控制器上。我的控制器中有一個js錯誤,導致我的控制器未正確初始化,並且作用域爲空。 感謝您的幫助。

+0

請問您可以共享代碼嗎? –