控制器和输入表单html使用var Javascript

Controller and input form html use var Javascript

本文关键字:使用 var Javascript html 表单 输入 控制器      更新时间:2023-09-26

我使用angularjs框架,创建了一个form.html和一个controller.js,其中包含一个变量,用于检索盒子的SSID。如何在表单中自动分配变量的值。这是一个输入字段。当启动应用程序时,表单应该自动显示SSID,而不需要用户这样做。

谢谢你的帮助。

'use strict';
angular.module('djoro.controllers')
  .controller('WifiSmartConfigCtrl', function ($scope, $window, $ionicPlatform) {
    $scope.getSSID = function () {
      var onSuccess = function (SSID) {
        document.write(SSID);
      };
      
      var onFail = function () {
        
      };
      $ionicPlatform.ready(function () {
        $window.cordova.plugins.Smartconfig.getSSID(onSuccess, onFail);
      });
    };
  });
<ion-pane>
  <ion-content ng-controller="WifiSmartConfigCtrl">
    <form novalidate class="simple-form">
      <fieldset>
        <legend>WI-FI</legend>
        <div class="list input-fields">
          <label class="item item-input">
            <span class="input-label">SSID :</span>
            <input type="text" name="test" value="getSSID()" required show-hide-input>
          </label>
          <label class="item item-input" show-hide-container>
            <span class="input-label">Password :</span>
            <input type="text" name="password" required show-hide-input>
          </label>
        </div>
      </fieldset>
    </form>
  </ion-content>
</ion-pane>

使用ng-model指令,这正是它的目的:

'use strict';
angular.module('djoro.controllers')
.controller('WifiSmartConfigCtrl', function($scope, $window, $ionicPlatform) {
  $scope.SSID = {};
  $scope.getSSID = function() {
      var onSuccess = function(SSID) {
          $scope.SSID = SSID;
      };
      var onFail = function() {};
      $ionicPlatform.ready(function() {
          $window.cordova.plugins.Smartconfig.getSSID(onSuccess, onFail);
      });
  };
});

和在你看来:

<input type="text" name="test" ng-model="SSID" required show-hide-input>

您需要在输入字段中添加一个ng-model,如下所示:

<label class="item item-input">
   <span class="input-label">SSID :</span>
   <input type="text" name="test" ng-model="SSID" required show-hide-input>
</label>

然后在控制器中分配SSID的值在$scope上:

$scope.SSID = [some_value]

查看这个PLNKR

正如你所看到的,我已经手动分配了SSID的值,你可以通过在你的函数的回调中分配它来动态地添加它,像这样:

$scope.SSID = {}
var onSuccess = function (SSID) {
  document.write(SSID);
  $scope.SSID = SSID
};