Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $scope提交表单时发生冲突_Angularjs - Fatal编程技术网

Angularjs $scope提交表单时发生冲突

Angularjs $scope提交表单时发生冲突,angularjs,Angularjs,我很难调试我没有编写的代码,也很难理解问题的根源(我不是一个角度开发人员)。 我有以下功能:一个主控制器,它初始化一个无线电对象:刷新窗口时调用它: $scope.radio_structure = { data: { localisations: [] }, streams: [] }; $scope.stream_structure = { url: "" };

我很难调试我没有编写的代码,也很难理解问题的根源(我不是一个角度开发人员)。 我有以下功能:一个主控制器,它初始化一个无线电对象:刷新窗口时调用它:

    $scope.radio_structure = {
        data: {
            localisations: []
        },
        streams: []
    };

    $scope.stream_structure = {
        url: ""
    };


    $scope.localisation_structure = {
        continent: "",
        country: "",
        city: ""
    }
    $scope.radio = angular.copy($scope.radio_structure);
还有一个提交表单数据的功能

    $scope.submit = function () {
        console.log($scope.radio);
        radioFact.add($scope.radio).then(
            function (success) {
                flashesFact.show(success.data);
                $location.url('/search');
            },
            function (error) {
                console.log(error);
                var response = {
                    type: 'danger',
                    msg: error.status + ' => ' + error.statusText
                }
                flashesFact.show(response);
            }
        );
    };
在我的add controller中,我进行了以下初始化:

    // Init stream data structure
    $scope.radio.streams.push(angular.copy($scope.stream_structure));
    // Init localisation data structure
    $scope.radio.data.localisations.push(angular.copy($scope.localisation_structure));
数据与ng模型绑定:

<div class="col-sm-10">
    <input id="name" type="text" name="name" class="form-control" ng-model="radio.data.name"
           ng-required="true"
           placeholder="{{'RADIO_NAME_PH' | translate}}">
    <span class="glyphicon glyphicon-ok form-control-feedback feedback-success"
          ng-show="suggest.name.$valid"></span>
    <span class="glyphicon glyphicon-remove form-control-feedback feedback-error"
          ng-show="suggest.name.$invalid"></span>

    <p ng-show="suggest.name.$error.required && !suggest.name.$pristine" class="help-block">
        {{'RADIO_NAME_MISERR' | translate}}
    </p>
</div>
问题如下:当我第一次提交表单数据时,它按预期工作。但是,在提交表单后,我可以决定发送一个新的表单,如果我这样做,并且我在表单中设置了新的值,则不会重置这些值,并且我会在第一次提交时提交相同的数据


我的理解是与作用域有冲突首先,在定义起始位置的地方创建一个init函数:

$scope.init = function() {
    $scope.radio_structure = {
        data: {
            localisations: []
        },
        streams: []
    };

    $scope.stream_structure = {
        url: ""
    };


    $scope.localisation_structure = {
        continent: "",
        country: "",
        city: ""
    }
    $scope.initRadio();
}

$scope.initRadio = function() {
    $scope.radio = angular.copy($scope.radio_structure);
}

$scope.init();
然后在成功提交后再次初始化:

$scope.submit = function () {
    console.log($scope.radio);
    radioFact.add($scope.radio).then(
        function (success) {
            flashesFact.show(success.data);
            $scope.initRadio();
            $location.url('/search');
        },
        function (error) {
            console.log(error);
            var response = {
                type: 'danger',
                msg: error.status + ' => ' + error.statusText
            }
            flashesFact.show(response);
        }
    );
};

首先,创建一个init函数,在其中定义起始位置:

$scope.init = function() {
    $scope.radio_structure = {
        data: {
            localisations: []
        },
        streams: []
    };

    $scope.stream_structure = {
        url: ""
    };


    $scope.localisation_structure = {
        continent: "",
        country: "",
        city: ""
    }
    $scope.initRadio();
}

$scope.initRadio = function() {
    $scope.radio = angular.copy($scope.radio_structure);
}

$scope.init();
然后在成功提交后再次初始化:

$scope.submit = function () {
    console.log($scope.radio);
    radioFact.add($scope.radio).then(
        function (success) {
            flashesFact.show(success.data);
            $scope.initRadio();
            $location.url('/search');
        },
        function (error) {
            console.log(error);
            var response = {
                type: 'danger',
                msg: error.status + ' => ' + error.statusText
            }
            flashesFact.show(response);
        }
    );
};

嗯,看起来不是示波器的问题。其中是触发
$scope.submit的
ng click
事件的元素?还有,什么是
radioFact
?代码在哪里?我添加了缺少的信息嗯,看起来不是范围。其中是触发
$scope.submit的
ng click
事件的元素?还有,什么是
radioFact
?代码在哪里?我添加了缺少的信息这给了我相同的结果,两倍于相同的数据你用ng点击执行操作,然后提交表单。表单标记没有添加到代码中,但我猜它会触发相同的函数。可能是因为这个原因我不太明白你说的话。ng click触发服务的“add()”方法(我在代码中添加了它),该方法从作用域获取单选对象,并通过POST请求将其提交到服务器。submit按钮具有
ng click=“submit($event)”
属性。因此,单击此按钮将触发提交方法并提交表单(
button type=“submit”
)尝试将按钮标记更改为div并删除type=“submit”属性。这会产生相同的结果,两次相同的数据您使用ng click执行操作并提交表单。表单标记没有添加到代码中,但我猜它会触发相同的函数。可能是因为这个原因我不太明白你说的话。ng click触发服务的“add()”方法(我在代码中添加了它),该方法从作用域获取单选对象,并通过POST请求将其提交到服务器。submit按钮具有
ng click=“submit($event)”
属性。因此,单击此按钮将触发提交方法并提交表单(
button type=“submit”
)尝试将按钮标记更改为div并删除type=“submit”属性。