javascript - 如何在 Angular 中定义 HTML block 的范围?

标签 javascript angularjs

我的应用程序页面中有一个非常大的表单,我有一个 JSON 变量,我在其中保存所有表单数据,我正在使用 controllerAs表达。

<form>
    <input type="text" data-ng-model="ctrl.myJSON.name"/>
    <input type="text" data-ng-model="ctrl.myJSON.old"/>
    <input type="text" data-ng-model="ctrl.myJSON.address"/>
    <input type="text" data-ng-model="ctrl.myJSON.email"/>
    <input type="text" data-ng-model="ctrl.myJSON.phone"/>
    <!-- and many more... -->
</form>

如何将 myJSON 定义为表单范围,因为不需要在所有字段中多次重复此变量?

最佳答案

您可以使用 angular extend 将 json 数据复制到 $scope。然后,您可以直接使用:

.controller('YourController', function($scope) {

    $scope.readData = function() {
        // replace with your read data function
        var myJSON = { name: 'test' };
        angular.extend($scope, myJSON);
    }

}

之后,变量将直接起作用:

<input type="text" data-ng-model="name"/>

关于javascript - 如何在 Angular 中定义 HTML block 的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027327/

相关文章:

javascript - 使用 Javascript 排序

javascript - 关于 Javascript getElementsByTagName 方法的问题

javascript - Opera 和 Internet Explorer 的调试解决方案

javascript - AngularJS : Services are not resolved in my controller

javascript - AngularJS 无法读取未定义的属性

javascript - 想要在输入字段中使用 AngularJs 禁用 91

angularjs - 如何在 Visual Studio 代码中调试 Angular 应用程序?

Javascript调用函数引用

javascript - 如何使用 Bootstrap Vue 在悬停时打开选择下拉菜单

angularjs - AngularJS 中的模块函数有什么作用?