在这种情况下,似乎不能让ng-model工作
Can't seem to get ng-model to work in this instance
<div class="col-lg-3">
<input type="text" id="hourHours" ng-model="hourHours" class="form-control" style="width: 60px" placeholder="hh" />
</div>
<button type="button" class="btn" ng-click="addSubscriptionModel()">submit</button>
下面是我的简单html代码和我的ng-model。如果我在input中输入5,angular代码应该是console.log out 5,但它仍然没有定义…
var SomeController = function ($scope, $modalInstance) {
$scope.addSubscriptionModel = function () {
console.log($scope.hourHours);
}
});
编辑:实际代码…似乎我忘了说我正在使用tabset,这可能是它不起作用的原因…
<tabset>
<tab heading="Hour" select="hourOption()" ng-model="hour">
<div class="col-lg-3">
<input type="text" id="hourHours" ng-model="hourHours" class="form-control" style="width: 60px" placeholder="hh" />
</div>
</tab>
</tabset>
终于想通了…从外部按钮
中清除与Angular Bootstrap UI选项卡内textarea关联的ng-modelJeffrey A Gochin的想法是对的…我确实需要在我的控制器中为我的两个对象设置"值"。
$scope.hour = {
hours: "",
minutes: ""
};
然后在我的按钮函数中,我可以访问$作用域。
这里没有看到足够的细节,但是当您的页面从数据源加载和填充时,您通常需要设置模型[scope]的属性来初始化您的页面。简而言之,您正在做的是双向绑定。因此,如果您希望在控制器中显示页面之前填充页面,则需要在执行控制器时设置作用域上的值。在您的页面中输入新的数据范围(或模型)后,它将自动更新为屏幕上的值。
更新作为后续。我了解到明智地使用$compile
和$interpolate
可以在很大程度上解决以下问题:它们允许您执行对作用域的后期绑定。此外,虽然我仍然不太了解它,完全了解何时使用它,transclusion
也可以有所帮助。
看看这段代码的工作,检查你的控制台窗口
<!DOCTYPE html>
<html lang="en" data-ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
</head>
<body ng-controller="MyCtrl">
<div class="col-lg-3">
<input type="text" id="hourHours" ng-model="hourHours" class="form-control" style="width: 60px" placeholder="hh" />
</div>
<button type="button" class="btn" ng-click="addSubscriptionModel()">submit</button>
</body>
</html>
<script>
var app = angular.module('app',[]);
app.controller('MyCtrl', function($scope){
$scope.addSubscriptionModel = function () {
console.log($scope.hourHours);
}
});
</script>
有透透性和$compile的东西。我通过在div标签中包含类似表单的内容而不是尝试动态加载它们来解决这个问题。你可以通读标签代码,然后谷歌这个问题。除了我描述的黑客之外,目前没有其他修复方法。
问题的关键是需要$compile选项卡内容后,它加载,但当前版本的选项卡不能正常工作。有一些递归问题会在错误的条件下导致堆栈溢出。Git上有一个公开的问题
相关文章:
- Cordova ng路线工作不正常
- 从 Javascript 中设置 ng-model name
- 使用jqGrid列格式化程序函数使ng-click工作
- ng重复工作但没有表现出表情
- ng src工作不正常,但src工作正常
- Toggle Switch在包含ng model和ng repeat之后不进行切换
- 在Angularjs中,我如何使用ng repeat、ng model和ng click来动态更改内容
- $localStorage array select with ng选项工作不正常
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- ng-model 绑定到选择标签生成的空默认值
- 对象不能在angularjs ng-model中访问
- Angularjs的ng-model不工作输入[text])
- AngularJS:在ng-if中输入ng-model不工作
- Angularjs的ng-model在使用自定义指令后无法工作
- Ng-model不能在ng-include中工作
- 为什么使用ng-model两次输入字段工作?
- Ng-model只有在我嵌套相同的控制器时才能工作
- 由于作用域问题,Ng-model不能工作
- AngularJS. ng-model不能工作
- 在这种情况下,似乎不能让ng-model工作