从AngularJS$scope ng模型返回对象
Return object from AngularJS $scope ng-model
我使用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>
您已经在获取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);
});
});
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;
}
}
相关文章:
- Angular,函数在(模型)工厂中返回值
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- Aurelia绑定-点击按钮返回查看模型
- 续集返回未定义的模型自定义方法
- “渲染骨干模型视图”返回未定义的结果
- Node.js应用程序中从模型层异步返回数据
- Ember.js返回相同的模型
- 保存模型返回错误
- Ember.js:路由返回多个模型,包括一个promise,给出错误
- 在控制器中将 ng 模型值返回为空的指令
- 在视图模型中使用 MVC 视图返回 JsonResult 数据
- 挖空映射对象未由杜兰达尔视图模型返回
- 在我的情况下,ng 模型返回未定义
- Knockout视图模型返回函数(如何?)
- 从AngularJS$scope ng模型返回对象
- 基于User的客户模型返回基于userId而不是customerId的FK属性
- 需要余烬模型返回数组承诺而不是对象
- 如何在 AngularJS 中将数据异步从模型返回到控制器
- Ember JS -无法循环从模型返回的数组,即使它是一个数组
- 从骨干模型返回承诺