简体   繁体   English

双向数据绑定不适用于控制器-AngularJS

[英]Two-Way Data Bind Not Applying with Controller - AngularJS

Good morning SO Gurus. 早上好,大师们。 Im having difficulty getting the two-way data binding working. 我无法使双向数据绑定正常工作。 The two way data binding is working in every other controller no problem, but this one is giving me a hard time. 数据绑定在其他所有控制器中都可以正常工作的两种方式,但这给我带来了麻烦。 Can you assist please? 你能帮忙吗?

Controller / HTML: 控制器/ HTML:

 app.controller('search', function($scope, $http, $location) { $scope.searchText = ""; $scope.searchRun = function() { $scope.userQuery = "Last::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+First::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+Display::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+User::Login+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+Manager::Display::Name+eq+"+$scope.searchText.replace(/ /g,"::"); $http({ method: 'GET', url: url }) .then(function(res) { $scope.TransmutedUsers = { users: [ ] }; //Looping through users making a new object for(var i = 0; i < res.data.users.length; i++) { //console.log(res.data.users[i].id); var obj = { id: res.data.users[i].id }; for(var x = 0; x < res.data.users[i].fields.length; x++) { //console.log(res.data.users[i].fields[x].name); var nameVar = res.data.users[i].fields[x].name.replace(/\\s+/g, ''); var valueVar = res.data.users[i].fields[x].value; obj[nameVar] = valueVar; } $scope.TransmutedUsers.users.push(obj); } console.log($scope.TransmutedUsers.users); $scope.returnedUsersLength = $scope.TransmutedUsers.users.length; console.log($scope.returnedUsersLength); sessionStorage.setItem("searchActive", "true"); if ($location.url() != "/search") { $location.url("/search"); } }); }; }); 
 <nav class="navbar navbar-default navbar-fixed-top bg-color--brand-blue"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a class="navbar-brand" href="#"><img src="/images/logo.png" class="logo" alt=""></a></li> <li><a href="/dashboard"><img src="/images/eAccess-logo-header.png" width="80" alt=""></a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="/profile">Welcome, <span>{{userName}}</span></a></li> <li><a href="/profile">Contact Help</a></li> <li><a href="/profile">Close Window</a></li> </ul> </div> <!--/.nav-collapse --> </div> </nav> <div class="sub-header bg-color--light-grey"> <div class="container-fluid"> <div class="row"> <div class="col-sm-6"> <div class="row"> <div class="col-sm-3"><a href="/newrequest" class="btn btn-primary btn-sm">Make a Request</a></div> <div class="col-sm-2 padding-right-none"><a href="/myprofile">My Profile</a></div> <div class="col-sm-2 padding-left-none"><a href="/directs">My Direct Reports</a></div> <div class="col-sm-2"><a href="/requests">My Requests</a></div> </div> </div> <div class="col-sm-6"> <form ng-controller="search" ng-submit="searchRun();" class="pull-right"> <input type="text" placeholder="Search for people or requests" ng-model="searchText"> <button type="submit"><i class="fa fa-search"></i></button> </form> </div> </div> </div> </div> <div class="container" id="Search" ng-controller="search"> <div class="row" id="SearchResultsUsers"> <div class="col-sm-12"> <div class="row"> <div class="col-sm-12"> <h1>Search Results: <i>{{searchText}}</i></h1> </div> </div> <div class="row" ng-repeat="user in TransmutedUsers.users"> <div class="col-sm-12"> <h3><i class="fa fa-user-circle"></i> <strong>{{user.FirstName}}</strong> {{user.LastName}} <small>({{user.UserLogin}})</small> <span>{{user.Role}}</span></h3> </div> <div class="col-sm-12"> <p>{{user.JobTitle}}, <a href="mailto:{{user.Email}}" ng-show="user.Email">{{user.Email}}</a> <span ng-show="user.Phone">{{user.Phone}}</span> Reports to: {{user.ManagerDisplayName}}</p> </div> <div class="col-sm-12"> <p>Direct Reports: <span ng-show="user.DirectReports">{{user.DirectReports}}</span><span ng-show="!user.DirectReports">None</span></p> </div> </div> </div> </div> </div> 

searchText does console.log but it does not bind to the {{searchText}} and the url does return the object successfully in the format specified, but that doesn't work with the {{}} either. searchText会进行console.log,但不会绑定到{{searchText}},并且url会以指定的格式成功返回对象,但是对{{}}也不起作用。

Using AngularJS 1.6 使用AngularJS 1.6

Please let me know if other information is required to help me solve this conundrum. 如果需要其他信息来帮助我解决这个难题,请告诉我。 Thank you all! 谢谢你们!

After declaring the $scope.searchText outside the search function, 在搜索功能之外声明$ scope.searchText之后,

you can force it by replacing the form : 您可以通过替换以下形式强制使用:

<form ng-controller="search" ng-submit="searchRun();" class="pull-right">
                    <input type="text" placeholder="Search for people or requests" ng-model="searchText">
                    <button type="submit"><i class="fa fa-search"></i></button>
                </form>

with something easy like: 像这样简单:

<input type="text" placeholder="Search for people or requests" ng-model="searchText">
 <button ng-click="searchRun(searchText)"><i class="fa fa-search"></i></button>

and inside your search function: 并在您的搜索功能内:

$scope.searchRun = function(searchText) {
        $scope.searchText = searchText;
        ...rest of ur code

You can also try to debug this using this, in otder to see the changes made in the input directly in the html. 您还可以尝试使用此工具进行调试,以便直接在html中查看输入中所做的更改。

<input type="text" ng-model="searchText">
{{searchText}}

and in your controller declare outside the function 并在您的控制器中声明函数外

$scope.searchText = "";

So the problem is your use of two ngControllers. 所以问题是您使用了两个ngControllers。

From Angularjs Docs 从Angularjs文档

When a Controller is attached to the DOM via the ng-controller directive, AngularJS will instantiate a new Controller object, using the specified Controller's constructor function. 当通过ng-controller指令将Controller附加到DOM时,AngularJS将使用指定的Controller的构造函数实例化一个新的Controller对象。 A new child scope will be created and made available as an injectable parameter to the Controller's constructor function as $scope. 将创建一个新的子作用域,并将其作为可注入参数提供给Controller的构造函数$ scope。

So try declaring one "search" controller via ng-controller higher up on you DOM structure so the Divs share this instance of the controller. 因此,请尝试在DOM结构的上方通过ng-controller声明一个"search"控制器,以便Divs共享该控制器实例。 Unlike angularjs services controllers are not singletons. 与angularjs服务不同,控制器不是单例的。

I have created a plunker for you to show the problem: 我为您创建了一个插销显示问题: http://plnkr.co/edit/J2Ho1uLq0jzuVLlX3BLG http://plnkr.co/edit/J2Ho1uLq0jzuVLlX3BLG

Changing the scope on one controller does not change the scope on the other 更改一个控制器的作用域不会更改另一个控制器的作用域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM