如何使用AngularJS$rootScope



我将三个$rootScope从过程控制器传递到分级控制器,因此根据$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);
        }

评级Ctrl.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) 
})

最新更新