Javascript 如何制作一个表单,但可以同时使用两个表单(Angular JS)

Javascript 如何制作一个表单,但可以同时使用两个表单(Angular JS),javascript,angularjs,forms,angularjs-scope,Javascript,Angularjs,Forms,Angularjs Scope,我有个问题。与模板angularjs没有太大区别。但我还是很困惑。我做了一个表格,用在两个必要的地方。必要的是更新表单和添加表单。如果标题更新数据(更新表单),则结果将如我所愿。但是,如果标题“添加数据”(添加表单)不起作用。问题是我调用$scope.label-->只是一个例子。结果是不确定的,我不知道怎么做。但如果在更新数据(更新表)是正确和良好的。有什么解决办法吗?或者我的逻辑模板是错误的?我该如何解决我的问题?谢谢 这是我的代码: FORM.HTML(部分我的模板) 控制器添加表单 $s

我有个问题。与模板angularjs没有太大区别。但我还是很困惑。我做了一个表格,用在两个必要的地方。必要的是更新表单和添加表单。如果标题更新数据(更新表单),则结果将如我所愿。但是,如果标题“添加数据”(添加表单)不起作用。问题是我调用
$scope.label
-->只是一个例子。结果是不确定的,我不知道怎么做。但如果在更新数据(更新表)是正确和良好的。有什么解决办法吗?或者我的逻辑模板是错误的?我该如何解决我的问题?谢谢

这是我的代码:

FORM.HTML(部分我的模板)

控制器添加表单

$scope.getDataTrendingTag = function (data){

        $scope.showId = false;
        $scope.showId = !$scope.showId;

        $scope.showButton = false;
        $scope.showButton = !$scope.showButton;

        $scope.getInclude = function() {
            return 'templates/form.html';
        };

        $scope.GetDataForm = function() {
            return 'templates/form-trending-tag.html';
        };

};
$scope.CreateTrendingTag = function() {

        $scope.showId = true;
        $scope.showId = !$scope.showId;

        $scope.showButton = true;
        $scope.showButton = !$scope.showButton;

        $scope.getInclude = function() {
            return 'templates/form.html';
        };

        $scope.GetDataForm = function() {
            $scope.label = "";
            $scope.active0 = {
                type: {
                    checked: false
                }
            };

            $scope.active1 = {
                type: {
                    checked: false
                }
            };
            $scope.slot = "";
            $scope.url = "";
            $scope.target = "";
            $scope.tracker = "";

            return 'templates/form-trending-tag.html';
        };
    };

这是因为在使用ng include时创建了一个新的子作用域

您可以做的是:

1) 使用对象而不是
$scope.label
您可以执行
$scope.data={}
操作,然后将
ng模型设置为
data.label

2) 使用
$parent
(不推荐使用)

像这样
ng model=“$parent.label”

如果在
ng include
中有
ng include
,则需要
$parent.$parent.label

希望这有帮助

您可以在这里阅读有关范围和ng incluce的内容


关于

什么不起作用了?$scope.data.label中是否仍有未定义的内容?是的,我在console.log中没有定义,所以您尝试了ng model=“data.label”和$scope.data={};console.log($scope.data.label)和ng model=“$parent.label”console.log($scope.label)?在这两种情况下,它都是未定义的?我建议使用console.log($parent)并查看parents中定义了哪些值。是的,我尝试了两种情况,但都没有定义。然后在控制台($parent)中显示“$parent未定义”。对不起,我在ng include中使用了ng include。应该是$parent.$parent。现在它开始工作了。谢谢你的帮助,先生:)
$scope.getDataTrendingTag = function (data){

        $scope.showId = false;
        $scope.showId = !$scope.showId;

        $scope.showButton = false;
        $scope.showButton = !$scope.showButton;

        $scope.getInclude = function() {
            return 'templates/form.html';
        };

        $scope.GetDataForm = function() {
            return 'templates/form-trending-tag.html';
        };

};
$scope.CreateTrendingTag = function() {

        $scope.showId = true;
        $scope.showId = !$scope.showId;

        $scope.showButton = true;
        $scope.showButton = !$scope.showButton;

        $scope.getInclude = function() {
            return 'templates/form.html';
        };

        $scope.GetDataForm = function() {
            $scope.label = "";
            $scope.active0 = {
                type: {
                    checked: false
                }
            };

            $scope.active1 = {
                type: {
                    checked: false
                }
            };
            $scope.slot = "";
            $scope.url = "";
            $scope.target = "";
            $scope.tracker = "";

            return 'templates/form-trending-tag.html';
        };
    };