简体   繁体   English

AngularJS / jQuery-更新范围

[英]AngularJS/jQuery - Update Scope

I am working on a legacy system that uses jQuery and had to add AngularJS for a particular feature, however im having issues updating the scope. 我正在使用jQuery的旧系统上工作,并且必须为特定功能添加AngularJS,但是我在更新范围方面遇到问题。

Basically, we have a dropdown and when you select an option, we're firing an Ajax call to get an array or objects. 基本上,我们有一个下拉菜单,当您选择一个选项时,我们将触发Ajax调用以获取数组或对象。 This array of objects is then being stored in a global variable, say var myObjs. 然后将此对象数组存储在全局变量中,例如var myObjs。 Basically using the ng-repeat service from Angular, I need to loop through these and render a list. 基本上使用Angular的ng-repeat服务,我需要遍历这些并呈现一个列表。

I am new to Angular, so i'm not sure if this is the way to be done. 我是Angular的新手,所以我不确定这是否是完成的方式。 What I am doing is setting the scope in this way: 我正在做的是这样设置范围:

$scope.myObjs= myObjs; $ scope.myObjs = myObjs;

However, by doing so, the scope is not changing at all. 但是,这样做根本不会改变范围。

Can someone tell me how this can be done? 有人可以告诉我该怎么做吗? I tried to look around but am finding it a bit hacky having a hybrid of AngularJS & jQuery on the same page. 我试图环顾四周,但在同一页面上混合了AngularJS和jQuery时发现它有点笨拙。

EDIT : adding sample snippet. 编辑 :添加示例代码段。 Basically on change of the dropdown im firing an ajax call, and store the response (which is an array of objects) in myObjs variable. 基本上在更改下拉菜单时会触发ajax调用,并将响应(对象数组)存储在myObjs变量中。 Then I am trying to set the scope to the value of this variable. 然后,我试图将范围设置为此变量的值。 Regarding the way I am bootstrapping Angular, this is due to a limitation of the legacy system, which is over 8 years old. 关于我引导Angular的方式,这是由于旧系统的限制,该系统已有8年的历史了。

 var myObjs = null; $(function() { $("#myHeader").on("change", "#mySelect", function() { // perform Ajax Call }); }); function ajaxCallback(data) { myObjs = data; } var myModule = angular.module("GetObjsModule", []); myModule.controller("MyController", function($scope) { $scope.objs = myObjs; }); angular.element(document).ready(function() { var myDiv = $("#myDiv"); angular.bootstrap(myDiv, ["GetObjsModule"]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script> <div id="myHeader"> <select id="mySelect"> <option value="1">Value 1</option> <option value="2">Value 2</option> <option value="3">Value 3</option> </select> </div> <div id="myDiv"> <ul id="myList" ng-controller="MyController"> <li ng-repeat="x in objs"> <div class="accordionHeader"> {{x.name}} {{x.surname}}: {{x.tel}} </div> <div> <p> Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate. </p> </div> </li> </ul> </div> 

It's pretty hard to come from jQuery and handle Angular's way of thinking. 从jQuery来处理Angular的思维方式非常困难。

Basically, to achieve what you want, you don't need jQuery at all. 基本上,要实现所需的功能,根本不需要jQuery。

<div ng-controller="MyController">
<div id="myHeader">
  <select id="mySelect" ng-change="yourUpdateFunction()">
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
    <option value="3">Value 3</option>
  </select>
</div>

<div id="myDiv" ng-show="requestLoaded">
  <ul>
    <li ng-repeat="x in objs">
      <div>
        {{x.name}} {{x.surname}}: {{x.tel}}
      </div>
      <div>
        <p>
          Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum
          a velit eu ante scelerisque vulputate.
        </p>
      </div>
    </li>
  </ul>
</div>
</div>

So basically : 所以基本上:

  1. I wrapped your code into a div, which will contain your controller 我将您的代码包装到div中,其中将包含您的控制器
  2. I used ngChange directive to run an update function on your select element 我使用ngChange指令在您的select元素上运行更新功能

Then, the JS part : 然后,JS部分:

var myApp = angular.module("myApp", []);
myApp.controller("MyController", function($scope, $http) {
  $scope.objs;
  $scope.requestLoaded = false;
  $scope.yourUpdateFunction = function () {
     // Here you can run a $http request, based on value of select.
     $http.get('/someUrl').success(function(data, status, headers, config) {
       $scope.objs = data;
       $scope.requestLoaded = true;
  });
  }
});

In a nutshell : $scope.yourUpdateFunction is run on change on your select element (ngChange directive). 简而言之: $scope.yourUpdateFunction在对select元素(ngChange指令)进行更改时运行。 It performs an asynchronous http request and set its response data to $scope.objs (so, from now, you can display it in the view). 它执行一个异步http请求,并将其响应数据设置为$scope.objs (因此,从现在开始,您可以在视图中显示它)。 Then, the $scope.requestLoaded is used to show / hide your list. 然后, $scope.requestLoaded用于显示/隐藏列表。 When the http request is loading, its false, the list is hidden, when it's done, the list is displayed, it's the directive ngShow . 当http请求正在加载时,它的false,列表被隐藏,完成后,显示列表,这是指令ngShow

It's a basic example, but this way you can achieve what you want. 这是一个基本示例,但是通过这种方式您可以实现所需的目标。

Remember that AngularJS provides a two-way data binding, whenever you change something in the view, it gets updated in the controller, and vice versa. 请记住,AngularJS提供了双向数据绑定,每当您在视图中更改某些内容时,它将在控制器中进行更新,反之亦然。

Give an id for the ul <ul id="Mylist"> and add the below code in your callback function. 为ul <ul id="Mylist">一个ID,并将以下代码添加到您的回调函数中。

 angular.element(document.getElementById('Mylist')).scope().$apply(function(scope){
     scope.objs = myObjs;
});

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

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