javascript - Angularjs 同一 Controller 的两种形式

标签 javascript html angularjs forms twitter-bootstrap

我的 angularjs 代码有问题。我有一个以两种模式复制的 Angular 色选择:创建用户和更改 Angular 色。 此选择通过以下方式填充 http 调用:

<ui-select theme="bootstrap" style="width: 100%;"
    data-ng-model="newUser.role" required> 
    <ui-select-match placeholder="Select role">
       {{$select.selected.role}}
    </ui-select-match> 
    <ui-select-choices
       repeat="role.idRole as role in (roles | filter: $select.search) track by role.role">
       <span data-ng-bind="role.role"></span>
    </ui-select-choices>
</ui-select>

在 Angular 上我有:

$http({
        method: 'GET',
        url: 'roles'
    }).then(function successCallback(response) {
        $scope.roles = response.data.result;
        // this callback will be called asynchronously
        // when the response is available
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

所以我把 HTML 代码放在两个模态的表单中。 在第一个中,创建用户,一切正常,在第二个中,我无法使用不同的 data-ng-model,因为否则我会收到未定义的元素。例如我有这个:

<form novalidate class="simple-form" name="newRoleForm">
    <!-- form start -->
    <div class="box-body">
        <div class="form-group">
            <label>New role</label>
            <!-- <ui-select theme="bootstrap" style="width: 100%;"
                data-ng-model="newRole.role" required> <ui-select-match
                placeholder="Select new role">
            {{$select.selected.role}}</ui-select-match> <ui-select-choices
                repeat="role.idRole as role in (roles | filter: $select.search) track by role.role">
            <span data-ng-bind="role.role"></span> </ui-select-choices> </ui-select> -->
            <ui-select theme="bootstrap" style="width: 100%;"
                data-ng-model="newUser.role" required> <ui-select-match
                placeholder="Select role">
            {{$select.selected.role}}</ui-select-match> <ui-select-choices
                repeat="role.idRole as role in (roles | filter: $select.search) track by role.role">
            <span data-ng-bind="role.role"></span> </ui-select-choices> </ui-select>
        </div>
    </div>
</form>

如果我使用注释代码,我不会在 javascript 中收到 Angular 色值,如果我使用未注释代码,它会起作用,因为 newUser 在其他模式中使用。 我可以使用这个实际的代码,但我想了解它为什么有效,而另一个则不行。 此外,当我打开第二个模态时,我会看到第一个模态中设置的值。 你知道为什么吗?

最佳答案

获得完美的概述有点棘手,但我想我会尝试回答您的一些问题:

  1. 为什么 newRole.role 不起作用,而 newUser.role 却起作用?

    据我所知,基本上你自己回答了这个问题。在开始将属性绑定(bind)到对象之前,您需要在范围内的某个位置定义该对象。即 Controller 中的一个简单的 $scope.newRole = {} 可能会修复此错误。

  2. 第一个模态的值是否设置在第二个模态中?

    我对此不确定,因为我看不到第二个模态(?)。但是,如果您在同一范围内重用相同的代码,则会将数据绑定(bind)到同一个 newUser 对象。因此每次都会得到相同的值。

我希望这能在某种程度上有所帮助

关于javascript - Angularjs 同一 Controller 的两种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35314099/

相关文章:

jquery - 选择奇数和偶数元素

javascript - 使用 roundabout.js 时如何知道 StartChild

html - css:使div变圆并在其3/4处添加边框

在 AngularJS 和 Testacular 中测试广播

javascript - 为单元格渲染器导出 ag-grid 中的数据

javascript - 对于对象循环?

Javascript worker.postMessage 缩小空数组

angularjs - 通过模型更改输入占位符的值?

javascript - 为什么这个 Javascript 无法找到正确的索引?

javascript - 在 amcharts 中单击时突出显示单个条形