[英]Is it possible to define ng-include, ng-init, ng-controller in controller
[英]Issue with ng-init and ng-controller
我在初始化全局變量以在mycontroller的所有函數中使用時遇到一些問題:
到目前為止,這是我所做的:
JS :
$scope.init = function (table) {
alert("alert 1 : " + table);
$scope.tableName = table;
alert($scope.tableName);
};
tableName = $scope.tableName;
alert("alert 2 : " + tableName);
HTML :
<div ng-init="init('RIGHTTABLE')" ng-controller="DropdownCaptionCtrl">
我的瀏覽器上有以下命令的警報:
警報2:未定義
警報1:RIGHTTABLE
您有解決此問題的想法嗎?
我想要的是按此順序發送警報:
警報1:RIGHTTABLE
警報2:RIGHTTABLE
實際上,我想在所有控制器功能中使用一個全局變量$scope.tableName
,我想在頁面啟動時對其進行初始化
謝謝
由於控制器最初已加載,因此在獲取未定義的變量后將調用您的init函數,您可以做的是
$scope.init = function (table) {
alert("alert 1 : " + table);
$scope.tableName = table;
alert($scope.tableName);
alert("alert 2 : " + $scope.tableName);
};
更新:
如果要在所有控制器中使用該變量,請進行Service
其他選擇是使用$ rootScope,
$scope.init = function (table) {
alert("alert 1 : " + table);
$rootScope.tableName = table;
alert($rootScope.tableName);
alert("alert 2 : " + $rootScope.tableName);
};
然后,您將可以使用$rootScope.tableName
在任何控制器中進行訪問
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.