簡體   English   中英

如何使用AngularJS $ rootScope?

[英]How to use AngularJS $rootScope?

我正在將三個$rootScope從過程控制器傳遞到Rating控制器,因此基於$rootScope狀態,我正在啟用和禁用按鈕。 編輯和查看工作正常,但在$rootScope === 'NewPrt' ,一旦用戶回答了所有問題,我想在'NewPrt'上啟用提交按鈕。

到目前為止,我嘗試下面的代碼。

HTML

<button type="submit" class="btn btn-default" ng-disabled="disabledDraft"  ng-click="savePRTDraft()" ng-show="showSaveDraftBtn">Save
        as Draft</button>
    <button type="submit" class="btn btn-primary"
        ng-disabled="disableSubmitButton" ng-click="submitClicked()">Submit</button>

ProcessCtrl.js

$scope.gotoQstnPage = function(isNew) {
        var qrtUrl = "/createRtgQstnAir/"+$scope.processDTO.processKey + "/"+isNew;
        $rootScope.status = 'NewPrt';
        $location.path(qrtUrl);
    }

$scope.editProcessRating = function(prcsSessionKey) {
            var prtUrl = "/getProcessRating/"+prcsSessionKey;
            $rootScope.status = 'edit';
            $location.path(prtUrl);

        }

        $scope.viewProcessRating = function(prcsSessionKey) {
          var prtUrl = "/getProcessRating/"+prcsSessionKey;
          $rootScope.status = 'view';
          $location.path(prtUrl);
        }

RatingCtrl.js

if(j > $scope.questionnaire.length){
              if($rootScope.status ==='edit') {
                $scope.disableSubmitButton = false;
                $scope.disabledDraft = false;
                $scope.showBusDecDropDown = true;
              }

 $scope.disabledDraft = function(){
        if($rootScope.status === 'view') {
          return true;
        }
        else {
          return false;
        }
      }
      if ($rootScope.status === "NewPrt" ) {
        $scope.disabledDraft = false;
      }

您可以嘗試這樣,而不是使用$rootScope

 var app = angular.module('myApp', []); app.controller('Controller', function ($scope) { }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='myApp' ng-controller="Controller"> <form name="myForm"> <input name="myText" type="text" ng-model="mytext" required /> <button ng-disabled="myForm.$invalid">Save</button> </form> </div> 

當兩個條件都成立時,啟用提交按鈕:

if ($rootScope ==='edit' || $rootScope ==='NewPrt') {
    $scope.disableSubmitButton = false;
}

如果要使用$ rootScope,則需要在要使用config的每個控制器中注入$ rootScope,在任何位置運行

像這樣

var app = angular.module('app');
app.config(function($rootScope){
  $rootScope.name = "Hello World"
});

app.controller('home',function($scope,$rootScope){
$scope.name = $rootScope.name;
alert($scope.name) 
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM