【问题标题】:Why is my textbox not displaying data from AngularJS controller为什么我的文本框不显示来自 AngularJS 控制器的数据
【发布时间】:2019-01-09 01:47:57
【问题描述】:

在我的应用程序中,我可以使用 console.log 验证是否选择了正确的信息,但尝试在表单文本框中显示此信息不起作用。谁能告诉我我做错了什么?

这是我视图的 HTML。

<table class="table table-stripped table-hover">
    <thead>
        <tr>
            <th>ID</th>
            <th>Title</th>
        </tr>
    </thead>
    <tbody>
        <tr data-ng-repeat="organization in organizations | filter:search" data-ng-click="navToEdit($index)">
            <td>{{organization.Id}}</td>
            <td>{{organization.Title}}</td>
        </tr>
    </tbody>
</table>

这是单击该列表中的项目将您带到的内容。

<div class="form-group">
    <label for="txtTitle">Title:</label>
    <input type="text" type="text" id="txtTitle" class="form-control" data-ng-model="organization.Title" />
</div>

这是我的控制器。

app.controller("organizationsCtrl", ["$scope", "$location", "$routeParams", "spService",    
    function ($scope, $location, $routeParams, spService) {    
        var listTitle = "Organizations";    
    
        $scope.editing = false;    
    
        //example populating Organizations    
        spService.getRecords(listTitle, "?$select=ID,Title").then(function (result) {    
            $scope.organizations = result;    
        });    
    
        $scope.navToAdd = function() {    
            $location.path("/organizations/add");    
        }    
    
        $scope.navToEdit = function(index) {    
            $scope.organization = $scope.organizations[index];    
            $location.path("/organizations/" + index);    
            console.log($scope.organization.Title);   
        }    
    }    
]);   

$scope.navToEdit 确实输出了正确的组织,但 txtTitle 的文本框没有显示任何内容。

请帮忙!!

【问题讨论】:

    标签: javascript jquery html angularjs


    【解决方案1】:

    如我所见,这里的问题是你在这里重定向到另一个页面,

    $location.path("/organizations/" + index);

    如果您想将数据从一个页面传输到另一个使用服务或本地存储,这将清除控制器中的现有范围。

    【讨论】:

    • 所以如果我使用服务来存储我的数据,我可以在页面加载之间回想一下吗?
    • 是的,你可以这样做
    • 我只是在阅读有关 $rootScope 的信息。它是如何工作的,或者有什么帮助?
    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2015-01-27
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多