触发表单在部分/控制器负载上自动提交

时间:2015-04-10 03:01:42

标签: angularjs angularjs-directive

我有一个用户登录我的AngularJS应用程序,一旦他们做了http.get,就会检索包含一些键值的数据集。其中一个是我需要发布到iframe的密钥,以便为正确的用户排队(基于密钥)。到目前为止,我有这个,它没有用。

HTML:

<form name="watchLiveForm" action="{{testingUrl}}" target="watch-live" method="post" ng-submit="controllsubmit()">
  <label for="key">Company Software Key:</label>
  <input type="text" name="key" id="key" ng-model="key">

  <input type="submit">
 </form>
 <iframe name="watch-live" ng-src="{{testingUrl}}" width="100%" height="600">    </iframe>

控制器:

app = angular.module('angularWebApp.indexController', []);

app.controller('indexController', function($scope, $http, $rootScope, $sce) {

  $scope.controllsubmit = function() {
    console.log("I was called!");
  };

  if($scope.user !== undefined) {
    if($scope.user.software_key.constructor === Array) {
      $http.get('http://URL/api/' + $scope.user.software_key[1] + '/remotes').
      success(function(data) {
        if(data.id === 'error') {
          console.log(data);
        } else {
          $scope.machineList = data;
          $scope.key = $scope.user.software_key[1];
          console.log($scope.user.software_key[1]);
          console.log($scope.key);
          $scope.testing = 'http://URL/settings';
          $scope.testingUrl = $sce.trustAsResourceUrl($scope.testing);
        }
      }).
      error(function(data) {
        alert(data);
      });
    }
  }

});

指令:

angular.module('angularWebApp.indexDirectives', [])
.directive('form', function() {
    return {
        require: 'form',
        restrict: 'A',
        link: function(scope, element, attributes) {
            var scopa = element.scope();
            if (attributes.name && scopa[attributes.name]) {
                scopa[attributes.name].$submit = function() {
                    // Parse the handler of submit & execute that.
                    var fn = $parse(attr.ngSubmit);
                    $scope.$apply(function() {
                        fn($scope, {$event: e});
                    });
                };
            }
        }
    };
});

用户登录后,我调用http.get并获取我传递给表单的soft_key(有效)。似乎让表单自动提交是问题,因为我想最终隐藏输入,所以他们不会像现在那样看到表单。非常感谢任何帮助!

0 个答案:

没有答案