从AngularJS $ scope ng-model

时间:2015-09-21 08:40:17

标签: javascript angularjs scope angularjs-scope ionic

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

$scope.SSID {}; return [object Oject] in input form ng-model="SSID"

谢谢你,帮助我。

Controller.js



/*Controller*/

'use strict';

angular.module('djoro.controllers')
.controller('WifiSmartConfigCtrl', function($scope, $window, $ionicPlatform) {a
    $scope.SSID = {};

    $scope.getSSID = function() {
        var onSuccess = function(SSID) {
            $scope.SSID = SSID;
            return SSID;
        };
      
        var onFail = function() {};

        $ionicPlatform.ready(function() {
            $window.cordova.plugins.Smartconfig.getSSID(onSuccess, onFail, $scope.SSID);
        });
    };
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<ion-pane>
    <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
    </ion-header-bar>
    <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" ng-model="SSID" id="SSID" placeholder="SSID" required show-hide-input>
                    </label>
                    <label class="item item-input" show-hide-container>
                        <span class="input-label">Password :</span>
                        <input type="passwprd" name="test" placeholder="Password" required show-hide-input>
                    </label>
                </div>
            </fieldset>
        </form>
    </ion-content>
</ion-pane>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您已在范围内定义了获取SSID的功能,但您从未通过UI调用。如果您希望UI自动拥有它,您需要在控制器初始化中调用它。

您的控制器代码如下所示:

.controller('WifiSmartConfigCtrl', function($scope, $window, $ionicPlatform) {a
    $scope.SSID = {};

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

    $ionicPlatform.ready(function() {
        $window.cordova.plugins.Smartconfig
            .getSSID(onSuccess, angular.noop, $scope.SSID);
    });
});

答案 1 :(得分:0)

    I use a plugin cordova "smartConfig"

/*plugin.js*/

        function Plugin(){}

    Plugin.alert = function(content){
          var onSuccess = function(){};
          var onFail = function(){};
          cordova.exec(onSuccess, onFail, 'SmartconfigPlugin', 'alert', [content]);
    };


    Plugin.getSSID = function(onSuccess, onFail){
          cordova.exec(onSuccess, onFail, 'SmartconfigPlugin', 'getSSID', []);
    };

    module.exports = Plugin;
  

SmartConfig.java

package fr.enersy.cordova.smartconfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import android.R;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.util.Log;
import com.integrity_project.smartconfiglib.SmartConfig;
import com.integrity_project.smartconfiglib.SmartConfigListener;
import com.pandaos.smartconfig.utils.NetworkUtil;
//import fr.enersy.cordova.smartconfig.mySmartconfigListener;   // TODO remove from config.xml

public class SmartconfigPlugin extends CordovaPlugin {

  // Variables declaration
  SmartConfigListener smartConfigListener;
  SmartConfig smartConfig;
  byte[] freeData;

  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    if("alert".equals(action)){
      final String content = args.getString(0);
      showAlert(content);
      callbackContext.success();
      return true;
    }

    else if("getSSID".equals(action)){
        String SSID = getSSID();
        callbackContext.success(SSID);
        return true;
      }

  }

  private String getSSID() {
      Log.i("--->> SmartconfigPlugin", "Enter getSSID");
      String SSID = (NetworkUtil.getWifiName(this.cordova.getActivity())).trim();   // TODO replace by something like: smartconfig_network_name_field.getText().toString().trim();
      Log.i("---------->> SmartconfigPlugin", "SSID: " + SSID);

      Log.i("---------->> SmartconfigPlugin", "Exit getSSID");
      return SSID;
  }

}