ng-include丢失数据angularjs

时间:2015-02-02 06:46:35

标签: javascript angularjs angularjs-ng-include

我是angularjs的新手,我在尝试在表单中使用ng-include时遇到问题。

HTML表单:

<form method="post" id="formQuestion" name="formQuestion" ng-submit="sendForm()" novalidate ng-controller="questionValidation">
<ul>

    <div class="slide-animate" ng-include="'/templates/default/partials/_fields/f1.html'"></div>
    <div class="slide-animate" ng-include="'/templates/default/partials/_fields/f2.html'"></div>

f1查看部分

<li class="text-center">

<div>{{data | json}}</div>

Controller questionForm(parent)

.controller('questionForm', function($scope, $routeParams) {
    $scope.data = {};
    $scope.data.productTypeUrl = $routeParams.stringUrl;
    ...

Controller questionValidation(child)

.controller('questionValidation', ['$scope', '$http', function ($scope, $http) {
    alert($scope.$parent.data.productTypeUrl);
    $scope.isSubmit = false;
    ...

警告进入控制器会显示正确的值,但是当我尝试将此值显示到de view f1.html时,该值不会显示。

我缺少什么?

提前感谢你。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您生成了无效的HTML,您将div放在ul中。因此可能在角度开始之前,您的浏览器修复了无效的HTML。在表格中拥有指令是一个类似的问题,有一些解决方案,第一个,用ulli更改为div替换divli标签另一个,使用指令来替换这些元素。

首先尝试替换它:

<form method="post" id="formQuestion" name="formQuestion" ng-submit="sendForm()" novalidate ng-controller="questionValidation">
<ul>

    <div class="slide-animate" ng-include="'/templates/default/partials/_fields/f1.html'"></div>
    <div class="slide-animate" ng-include="'/templates/default/partials/_fields/f2.html'"></div>

用这个:

<form method="post" id="formQuestion" name="formQuestion" ng-submit="sendForm()" novalidate ng-controller="questionValidation">
<ul>

    <li class="text-center slide-animate" ng-include="'/templates/default/partials/_fields/f1.html'"></li>
    <li class="text-center slide-animate" ng-include="'/templates/default/partials/_fields/f2.html'"></li>

和f1查看:

<div>{{data | json}}</div>