【问题标题】:$scope isn't changed in AngularJS$scope 在 AngularJS 中没有改变
【发布时间】:2015-11-14 02:47:46
【问题描述】:

我正在写一个调度服务,所以每周有很多次;当您每次单击其 Unix 格式将发送到模态并且用户可以预订时,现在我想通过编写以下代码将 Unix 时间传递给 ngDialog 模块。

截图

预约服务一览

当你点击 9:00 按钮时,modal(ngDialog) 将被打开

我想在上面输入 Unix 时间。

控制器

schedule.controller('schedule', ['$scope', '$http', 'ngDialog', function($scope, $http, ngDialog) {
$http.get('/zt-api/business/admin/' + window.location.pathname.split('/')[2]).success(function(data) {
        $scope.admin_times = data;
        $scope.duration = {
            startTime: "",
            endTime: ""
        };
        $scope.clickToOpen = function(start) {
            ngDialog.openConfirm({
                template: '/static/partials/staff_admin/ngdialog/admin_block_time.html',
                scope: $scope
            }).then(function(value) {
                console.log(value);
            }, function(reject) {
                console.log(reject);
            });
            $scope.duration.startTime = start;
            console.log($scope.duration)
        };
    }
});
});
}]);

HTML

<div class="morning"  ng-hide="hideMorning" ng-repeat="(key4, array4) in array3.times | filter: {'part': 'morning'}">
    <div class="daytime" ng-click="clickToOpen(array4.unix)" id="{[{ array4.unix }]}" ng-show="array4.part=='morning'">{[{ array4.timestamp.split(" ")[1].slice(0,5) }]}</div>
</div>

**array4.unix returns Unix time.**

我希望当我运行 clickToOpen 函数时,它能够将$scope.duration.startTime 更改为start,但是,它只改变了不到一秒钟!如何修改?

【问题讨论】:

  • 好吧...我没抓住重点。你能说得更具体点吗?
  • 我只想把每个元素的$scope.duration.startTime的值改成array4.unix,就这样,但它不会永远改变,它只是改变了大约500ms!跨度>
  • 不知道为什么要使用字符串初始化 startTime,然后为其分配一个数值。您的问题和代码有点混乱;你能用一些上下文信息或一个实际例子来澄清一下吗?

标签: javascript angularjs angularjs-directive angularjs-scope


【解决方案1】:

如果你将控制器添加到 ngDialog.openConfirm() 并像这样记录 $scope。

ngDialog.openConfirm({
            template: 'templateId',
            controller: function($scope){
              console.log($scope);
            },
            scope: $scope
        }).then(function(value) {
            console.log(value);
        }, function(reject) {
            console.log(reject);
        });

}

您会注意到通过 $scopes.$parent 对象可以访问通过父控制器传入的任何值,如下图所示。这是一个plunk,它可以将 $scope 传递给 ngDialog 并在 ngDialog 的输入框中显示主控制器 $scope.dateTime

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多