在angularjs中单击new时,文本框更改为启用

时间:2014-09-24 15:11:08

标签: angularjs

我有一个带有bootstrap模式的表单,其中modal有一个文本框并添加了电子邮件按钮。如果您键入正确的电子邮件并单击“添加电子邮件”按钮,则该按钮将更改为删除电子邮件按钮和文本框模式设置为禁用模式和另一个新文本。但是当您单击“删除电子邮件”按钮时,所有文本框都将更改为启用模式。

see my code here

我的HTML代码

<!doctype html>
<html ng-app="app" ng-controller="ModalDemoCtrl">
<head>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
<script src="script.js"></script>
</head>

<div >
  <script type="text/ng-template" id="myModalContent">
      <div class="modal-header">
          <h3 class="modal-title">I'm a modal!</h3>
      </div>
      <div class="modal-body">
          <li ng-repeat="item in items " ng-form="subForm">
              <input type="text" name="name" ng-model="item.email"  ng-disabled="isDisable(disbl,$index)" required ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/"  ng-enter="addOrRemove($index,item.email)"/>
              <span ng-show="subForm.name.$invalid || subFform.name.$pristine" style="color: red">Invalid email</span>
              <button ng-disabled="subForm.name.$invalid || subFform.name.$dirty" ng-click="addOrRemove($index,item.email)" >{{item.value}}</button>
               expression: {{subForm.name.$invalid}}
          </li>

      </div>
      <div class="modal-footer">
          <button class="btn btn-primary" ng-click="ok()">OK</button>
          <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
      </div>
  </script>

  <button class="btn btn-default" ng-click="open('lg')" >Large modal</button>
</div>
</body>
</html>

我的脚本代码

var app = angular.module('app', ['ui.bootstrap']);
//var ModalDemoCtrl = function ($scope, $modal, $log) {
app.controller('ModalDemoCtrl',['$scope', '$modal','$log','$rootScope',
function controller($scope, $modal, $log, $rootScope)
{
  $scope.open = function (size) {
$scope.val = "";
var modalInstance = $modal.open({
  templateUrl: 'myModalContent',
  controller: ModalInstanceCtrl,
  size: size,
  backdrop: 'static',
  resolve: {
    items: function () {
      return $scope.items;
    }
  }
});

modalInstance.result.then(function (selectedItem) {
  $scope.selected = selectedItem;
}, function () {
  $log.info('Modal dismissed at: ' + new Date());
});

};     }]);

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function ($scope,$rootScope, $modalInstance) {
$scope.check2 = "hllo";

$scope.items = [
    {
        value: "Add email",
        state: "1",
        email: ""
    }];
$scope.check1;


$scope.addOrRemove = function(indexSelected,rcvEmail)
{//alert($rootScope.email1);

    if(!rcvEmail)
    {
        return
    }
    //console.log("just check email",rcvEmail);
    //console.log("length of the object",$scope.items.length);
    event.preventDefault();
    if($scope.items[indexSelected].state == 1)
    {
        $scope.isDisable = function(value,index)
        {
            if(index <= indexSelected)
            {
                //console.log(index);
                return value = "yes";
            }

        };
        //console.log($scope.items[indexSelected].state);
        $scope.items[indexSelected].value = "Remove email";
        $scope.items[indexSelected].state = "0";
        $scope.items[indexSelected].email = rcvEmail;
        $scope.items.push({value: "Add email", state: "1"});

    }
    else
    {
       $scope.items.splice(indexSelected, 1);

        $scope.isDisable = function(value,index)
        {
           console.log("indexes",index,"+",indexSelected);
            /*index = index-1;
            if(index <= $scope.items.length)
            {
                //console.log(index);
                return value = "yes";

            }
            else
            {
                return value = "";
            }*/
        };
    }

};

$scope.ok = function () {
$modalInstance.close();
};

$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};

};


app.directive('ngEnter', function () {
console.log("directive checke");
return function (scope, element, attrs) {
    element.bind("keydown keypress", function (event) {
        if(event.which === 13) {
            scope.$apply(function (){
                scope.$eval(attrs.ngEnter);
            });

            event.preventDefault();
        }
    });
};
});

see my code here

1 个答案:

答案 0 :(得分:1)

将ng-disabled设置为

   (items[$index].state == 0) ? true:false

而不是功能。