本文介绍了在$范围的变量是不确定的,除非我强迫它从服务中再次检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是关于一个角度控制器的code片段。我试图汲取这次 GitHub的项目

可疑的部分是位于函数 addStock 。我已经定义了$ scope.watchlist在initilizations部分,但是,如果我删除里面的重新声明 $ scope.addStock 功能,$ scope.watchlist不会填入正确的价值观。任何角度专家可以点我吗?
如果你想看到我的整个项目code - 这里是

  angular.module('stockCatApp')
  .controller('WatchlistCtrl',函数($范围,$ routeParams,$莫代尔,WatchlistService,CompanyService){
    //初始化
    $ scope.companies = CompanyService.query();
    $ scope.watchlist = WatchlistService.query($ routeParams.listId);
    $ scope.stocks = $ scope.watchlist.stocks;
    $ scope.newStock = {};
    VAR addStockModal = $模式({
      适用范围:$范围,
      模板:意见/模板/ addstock-modal.html',
      显示:假的
    });    $ scope.showStockModal =功能(){
      addStockModal $ promise.then(addStockModal.show)。
    };    $ scope.addStock =功能(){
      //下面的行需要再次被放到这里,以便检索正确的价值
      $ scope.watchlist = WatchlistService.query($ routeParams.listId);
      ////////////////////////////////////////////////// /////////////////////
      $ scope.watchlist.addStock({
        listId:$ routeParams.listId,
        公司:$ scope.newStock.company,
        股票:$ scope.newStock.shares
      });
      addStockModal.hide();
      $ scope.newStock = {};
    };
  });


解决方案

这看起来好像你是从一个模态调用$ scope.addStock()。在这种情况下,模态不一定继承父范围。这取决于一切的哪个版本所使用。它看起来像您正在使用角带(我看着你bower.json)。如果您在自己的$模态源$ C ​​$看看c您会发现这样的:

VAR范围= $模式$范围= options.scope&放大器;&安培; options.scope $新的()|| $ rootScope $新();

所以,当你打开你的模式,你正在创建一个新的作用域。您可能能够使用$父虽然访问原始范围。

Here is the code snippet about an Angular Controller. I'm trying to learn Angular from this github project

The questionable part is located in function addStock. I have already defined $scope.watchlist in the initilizations part, however, if I remove the re-declaration inside $scope.addStock function, $scope.watchlist will not be populated with right values. Can any Angular experts point me out?If you want to see my full project code - here is the link.

angular.module('stockCatApp')
  .controller('WatchlistCtrl', function ($scope, $routeParams, $modal, WatchlistService, CompanyService) {
    // Initializations
    $scope.companies = CompanyService.query();
    $scope.watchlist = WatchlistService.query($routeParams.listId);
    $scope.stocks = $scope.watchlist.stocks;
    $scope.newStock = {};
    var addStockModal = $modal({
      scope: $scope,
      template: 'views/templates/addstock-modal.html',
      show: false
    });

    $scope.showStockModal = function () {
      addStockModal.$promise.then(addStockModal.show);
    };

    $scope.addStock = function () {
      //The following line needs to be put here again in order to retrieve the right value
      $scope.watchlist = WatchlistService.query($routeParams.listId);
      ///////////////////////////////////////////////////////////////////////
      $scope.watchlist.addStock({
        listId: $routeParams.listId,
        company: $scope.newStock.company,
        shares: $scope.newStock.shares
      });
      addStockModal.hide();
      $scope.newStock = {};
    };
  });
解决方案

It appears as if you are calling $scope.addStock() from a modal. In which case the modal does not necessarily inherit the parent scope. It depends on which versions of everything you are using. It looks like you are using angular-strap (I looked in your bower.json). If you look in their $modal source code you will find this:

https://github.com/mgcrea/angular-strap/blob/master/src/modal/modal.js

var scope = $modal.$scope = options.scope && options.scope.$new() || $rootScope.$new();

So, when you are opening your modal you are creating a new scope. You might be able to access the original scope using $parent though.

这篇关于在$范围的变量是不确定的,除非我强迫它从服务中再次检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:15
查看更多