【问题标题】:Problems with ng-modelng-model 的问题
【发布时间】:2014-03-12 01:56:59
【问题描述】:

我有一个带有 3 个选项卡的应用程序,每个选项卡都从外部文件加载自己的模板。第一个选项卡中的 ng-model 未发送到第三个选项卡的问题。

第一个文件:

<div class="row">
  <div class="span2 text-right">*Reported By:</div>
  <div class="span2"><input type="text" ng-model="date" required></div>
  <div class="span2 text-right">*Well Number:</div>
  <div class="span2">
    <select ng-model="well" required ng-change="wellFunc(well)" required>
      <option ng-selected>Well-01</option>
      <option>Well-02</option>
      <option>Well-03</option>
    </select>
  </div>
</div>

第二:

<table class="table table-hover table-striped">
  <tr>
    <th><strong>General Information:</strong></th>
  </tr>
  <tr>
    <td  ng-model="date"></td>
  </tr>
</table>

我也用了ui-router,可能是路由器有问题?

var myApp = angular.module('myApp', ["ui.router"])
myApp.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise("/re");
$stateProvider
.state('re', {
  url: "/re",
  templateUrl: "template/general.html"
})
.state('ro', {
  url: "/ro",
  templateUrl: "template/corrective.html"
})
.state('ri', {
  url: "/ri",
  templateUrl: "template/result.html"
})

});

【问题讨论】:

    标签: javascript angularjs angular-ngmodel


    【解决方案1】:

    我需要查看您的控制器以确认,但您可能会破坏您的约会。在您的控制器中,我假设您有类似

    $scope.date = "03/11/2014";
    

    相反,做这样的事情

    $scope.foo = { "date":"03/11/2014"}
    

    并将您的 html 更新为

    <input type="text" ng-model="foo.date" required>
    

    现在,当用户更新输入时,它不会破坏其他“日期”引用,而只是更新 foo 引用的日期属性。查看此快速视频以获取说明。 https://egghead.io/lessons/angularjs-the-dot

    【讨论】:

    • 谢谢它的帮助。但我还有另一个问题。在日期我有日期选择器。当我在日期选择器中单击日期时,日期不再发送,但如果我编辑输入值一切正常。
    • 这应该是一个单独的问题,以便其他人受益。包括您的源代码和您正在使用的日期选择器。不过我的猜测是,日期选择器不是角度指令,因此事件发生在 Angular 世界之外,它不知道值的变化。查看stackoverflow.com/questions/15112584/…
    • 这是对 $scope.$apply() 的解释,它也可能有所帮助。 jimhoskins.com/2012/12/17/angularjs-and-apply.html
    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多