为什么我的控制器中数组的更改没有反映在我的视图中? [英] Why are changes in my arrays in my controller not being reflected in my view?

查看:19
本文介绍了为什么我的控制器中数组的更改没有反映在我的视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的分支:如何使用controller as"语法调用 $scope.$apply()

我将 TypeScript 与 Angular 结合使用

问题:我认为有一个 ng-repeat

 <div ng-hide="wordTrack.showDetailedView"><div class="col-md-12" id="wordTrackResult" style="padding: 10px; overflow-y: auto; overflow-x: hidden;"><div class="wordListWellWrapper row" ng-repeat="wordTrack.wordList 中的单词"><div class="col-md-5 wordListWell form-control" ng-class="(words.IsPositive)? 'posWordWell': 'negWordWell' "><strong class="wordListWord">{{words.Word}}</strong><div class="wordListIcon"><div class="whiteFaceIcon" ng-class="(words.IsPositive)?'happyWhiteIcon': 'sadWhiteIcon'"></div>

<div class="col-md-2"><span aria-hidden="true" class="glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words.Word)"></span>

<p class="noWordText" ng-show="(wordTrack.notUsedWordList.length > 0)">以下词还没有使用</p><div class="wordListWellWrapper row" ng-repeat="wordTrack.notUsedWordList 中的单词"><div class="col-md-5 wordListWell form-control" style="background-color: gray;"><strong class="wordListWord">{{words}}</strong>

<div class="col-md-2"><span aria-hidden="true" class=" glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words)"></span>

在我的控制器中使用了两个数组(wordList 和 notUsedWordList):

 模块 WordsToTrackController {导出类控制器{public static $inject = ["$scope", "CampaignFactory", "VideoFactory", "DashboardFactory", "WordsToTrackFactory"];wordList: Array;notUsedWordList: Array;构造函数($scope:ng.IScope,campaignFactory,videoFactory,dashboardFactory,wordsToTrackFactory){this.campaignFactory = campaignFactory;this.videoFactory = videoFactory;this.dashboardFactory = 仪表板工厂;this.wordsToTrackFactory = wordsToTrackFactory;this.wordList = [];this.notUsedWordList = [];this.hasData = false;this.fetchInProgress = false;$scope.$on("视频切换",() => {this.setWordLists();});$scope.$on("detail-switch",() => {this.showDetailedView = this.dashboardFactory.isDetailedView;});}}}

在我的构造函数中,我正在调用

 $scope.$on("video-switch",() => {this.setWordLists();});

它执行 setWordLists() ,它试图从我的一个工厂中获取数据并设置数组的值(它正确执行)

控制器:

 setWordLists() {this.fetchInProgress = true;var campaignId = this.campaignFactory.currentId();var videoId = this.videoFactory.currentId();如果 (!campaignId || !videoId) {返回;}this.wordsToTrackFactory.doGetWordsToTrackModel(campaignId, videoId).then((响应) => {this.fetchInProgress = false;this.wordList = (响应) ?response.data.WordList : [];this.notUsedWordList = (响应) ?response.data.NotUsedWords : [];});}

工厂:

 function doGetWordsToTrackModel(campaignId: number, videoId: number) {返回 $http.get(网址,{参数:{视频ID:视频ID,广告系列 ID:广告系列 ID}});}

现在,问题是即使数组被设置为正确的值,在我看来(在 ng-repeat 中)它并没有被更新.

我也不想调用 $scope.$apply() 来快速修复.谁能找出问题的根源?

$scope.$apply() 没有帮助,所以我认为 ng-repeat 没有正确绑定,但我看不出我是如何错误地设置视图的.

........

解决方案

我是个白痴,把我的 'ng-controller="Controller as controller" ' 放在了不正确的元素上.......

错误代码:

 

更正代码:

 

<h2 class="text-center">要跟踪的单词</h2>

This is branching off of my last question: How to call $scope.$apply() using "controller as" syntax

I am using TypeScript in conjunction with Angular

Problem: I have an ng-repeat in my view

    <!-- Simple View -->
<div ng-hide="wordTrack.showDetailedView">
    <div class="col-md-12" id="wordTrackResult" style="padding: 10px; overflow-y: auto; overflow-x: hidden;">

        <div class="wordListWellWrapper row" ng-repeat="words in wordTrack.wordList">
            <div class="col-md-5 wordListWell form-control" ng-class="(words.IsPositive)? 'posWordWell': 'negWordWell' ">
                <strong class="wordListWord">{{words.Word}}</strong>
                <div class="wordListIcon">
                    <div class="whiteFaceIcon" ng-class="(words.IsPositive)? 'happyWhiteIcon': 'sadWhiteIcon' "></div>
                </div>
            </div>
            <div class="col-md-2">
                <span aria-hidden="true" class="glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words.Word)"></span>
            </div>
        </div>

        <p class="noWordText" ng-show="(wordTrack.notUsedWordList.length > 0)">The following words have not yet been used</p>

        <div class="wordListWellWrapper row" ng-repeat="words in wordTrack.notUsedWordList">
            <div class="col-md-5 wordListWell form-control" style="background-color: gray;">
                <strong class="wordListWord">{{words}}</strong>
            </div>
            <div class="col-md-2">
                <span aria-hidden="true" class=" glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words)"></span>
            </div>
        </div>

    </div>
</div>

which is using two arrays in my controller (wordList and notUsedWordList):

 module WordsToTrackController {
export class Controller {
    public static $inject = ["$scope", "CampaignFactory", "VideoFactory", "DashboardFactory", "WordsToTrackFactory"];
    wordList: Array<IWordTrackItem>;
    notUsedWordList: Array<string>;


    constructor($scope: ng.IScope, campaignFactory, videoFactory, dashboardFactory, wordsToTrackFactory) {
        this.campaignFactory = campaignFactory;
        this.videoFactory = videoFactory;
        this.dashboardFactory = dashboardFactory;
        this.wordsToTrackFactory = wordsToTrackFactory;

        this.wordList = [];
        this.notUsedWordList = [];

        this.hasData = false;
        this.fetchInProgress = false;

        $scope.$on("video-switch",() => {
            this.setWordLists();
        });

        $scope.$on("detail-switch",() => {
            this.showDetailedView = this.dashboardFactory.isDetailedView;
        });
    }}}

Inside my constructor, I am calling

        $scope.$on("video-switch",() => {
            this.setWordLists();
        });

which executes setWordLists() which attempts to grab the data from one of my factories and set the values of the arrays (which it is doing correctly)

Controller:

   setWordLists() {
        this.fetchInProgress = true;
        var campaignId = this.campaignFactory.currentId();
        var videoId = this.videoFactory.currentId();

        if (!campaignId || !videoId) {
            return;
        }

        this.wordsToTrackFactory.doGetWordsToTrackModel(campaignId, videoId)
            .then((response) => {
            this.fetchInProgress = false;
            this.wordList = (response) ? response.data.WordList : [];
            this.notUsedWordList = (response) ? response.data.NotUsedWords : [];
        });
    }

Factory:

    function doGetWordsToTrackModel(campaignId: number, videoId: number) {
        return $http
            .get(url, {
            params: {
                videoId: videoId,
                campaignId: campaignId
            }
        });
    }

Now, the problem is that even though the arrays are being set to the correct values, it is not being updated in my view (inside the ng-repeat).

I don't want to call $scope.$apply() for a quick fix either. Can anyone spot the source of the problem?

Edit: $scope.$apply() doesn't help, so i'm thinking ng-repeat isn't binding correctly, but I can't see how I set the view up incorrectly.

Edit: ........

解决方案

I'm a moron and put my ' ng-controller="Controller as controller" ' on the incorrect element.......

wrong code:

                            <div class="col-md-4" >
                        <h2 class="text-center" ng-controller="WordsToTrackController as wordTrack">Words to Track</h2>

corrected code:

                            <div class="col-md-4" ng-controller="WordsToTrackController as wordTrack">
                        <h2 class="text-center">Words to Track</h2>

这篇关于为什么我的控制器中数组的更改没有反映在我的视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆