$scope在Angular JS函数中没有绑定
$scope not binding inside function in Angular JS
我正在使用ionic框架,并试图向服务发出请求,但我无法将参数传递给函数。
我的Html(离子结构)如下:<ion-view view-title="Service Factor" ng-controller="beamdeflectionCtrl">
<ion-content>
<div class="item">
<form novalidate class="col-lg-2">
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">Shaft Length</span>
<input type="number" name="itsShaftLength" placeholder="1212" ng-model="itsShaftLength">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Running Load</span>
<input type="number" placeholder="1212" ng-model="itsRunningLoad">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Area Moment Of Inertia</span>
<input type="number" placeholder="1212"
ng-model="itsAreaMomentOfInertia">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Modulus Of Elasticity</span>
<input type="number" placeholder="1212"
ng-model="itsModulusOfElasticity">
</label>
</div>
<div class="form-group">
<button class="btn btn-success btn-lg" ng-click="post()">Get Beam Defection</button>
</div>
</form>
</div>
</ion-content>
</ion-view>
和我的JS文件:
angular.module('beamdeflection', [])
.controller('beamdeflectionCtrl', beamdeflectionCtrl,['$scope']);
function beamdeflectionCtrl($stateParams, $scope, $http, $ionicPopup) {
$scope.post = function () {
$http({
url: "/myurl",
method: "GET",
params: {
shaftLength: $scope.itsShaftLength,//I am not getting this value
runningLoad:$scope.itsRunningLoad,//I am not getting this value
areaMomentOfInertia: $scope.itsAreaMomentOfInertia,//I am not getting this value
modulusOfElasticity:$scope.itsModulusOfElasticity//I am not getting this value
}}).success(function (data, status, headers, config) {
$ionicPopup.alert({
title: 'Beam Deflection Calculated',
template: data
});
$scope.data = data;
}).error(function (data, status, headers, config) {
$ionicPopup.alert({
title: 'Beam Deflection Failed',
template: data
});
});
};
};
}
由于某些原因,$scope.itsShaftLength
和post函数中的其他参数没有获得值。调试器声明它们是未定义的。我是角的新手。我想知道我是否在代码中遗漏了什么。此外,我试图将$scope传递给函数作为$scope.post = function ($scope){....
,但它向我大喊"$scope未定义"。
正如我在评论中提到的,你应该总是在ng-model
中使用对象,因为原语的绑定在子作用域中会被破坏。
一般经验法则: ng-model
尝试改变所有的ng-model
有一个对象前缀和一个点
<input ng-model="formData.itsModulusOfElasticity">
在控制器中定义对象:
$scope.formData={};
这也简化了你需要发送的参数,因为所有的表单数据已经包含在一个对象中:
params: angular.copy($scope.formData) // using "copy" strips out any angular properties
从这里开始进一步阅读,并确保阅读角作用域
为什么AngularJS文档不在model指令中使用点?
您确实没有在代码的$scope中定义ng-models。你可以这样做:
$scope.post = function (data) {
$http({
url: "/myurl",
method: "GET",
params: {
shaftLength: data.itsShaftLength,
runningLoad: data.itsRunningLoad,
areaMomentOfInertia: data.itsAreaMomentOfInertia,
modulusOfElasticity: data.itsModulusOfElasticity }})... more code...
在你的html中定义ng-model:
<input type="number" name="itsShaftLength" placeholder="1212" ng-model="data.itsShaftLength"> ...
和点击:
ng-click="post(data)"
相关文章:
- 如何在动态创建的节点上绑定函数
- 绑定函数时在IE7中未实现Javascript错误
- jQuery结合了2个绑定函数(类型错误:a是未定义的)
- Jquery函数无法处理动态加载的内容(不是绑定函数)
- 从console.log调用webpack绑定函数/类
- React没有绑定函数调用,尽管有相反的警告
- jQuery使用最后一个参数在Javascript循环中单击绑定函数
- 如何在React Native中为ListView中的每个元素绑定函数
- jquery绑定函数不起作用
- 具有绑定与未绑定函数的数组筛选器
- 绑定函数仅适用于最后一个元素
- 如何将新函数绑定到元素的 onclick 事件,并使其在所有其他绑定函数之前运行
- 绑定函数链接
- 动态 JS 拖动绑定函数限制在线拖动
- 绑定函数的返回语句中参数的推理是什么
- 在jQuery上动态绑定函数
- 确定 JavaScript 函数是否为绑定函数
- jQuery 绑定函数不起作用
- 反应组件中的绑定函数
- Javascript 绑定函数到 Ajax 加载事件