angularJS resolve: in controller "Cannot read 属性 'goals' of undefined

angularJS resolve: in controller "Cannot read property 'goals' of undefined

这是我关于 Whosebug 的第一个问题。 (我知道我对引号很费劲。)我遇到了这个问题,下面的代码片段给我留下了以下错误:

TypeError: Cannot read property 'goals' of undefined
at $scope.addValue.$modal.open.resolve.goals (app.js:59) at Object.invoke (angular.js:3762) at ui-bootstrap-tpls-0.12.1.js:2118 at Object.forEach (angular.js:329) at getResolvePromises (ui-bootstrap-tpls-0.12.1.js:2116) at Object.$modalProvider.$get.$modal.open (ui-bootstrap-tpls-0.12.1.js:2151) at Scope.$scope.addValue (app.js:53) at Parser.functionCall (angular.js:10294) at angular.js:18229 at Scope.$get.Scope.$eval (angular.js:12075)

'goals' 在我的代码中相当于 'items' 在“受启发”代码中。

此消息似乎来自包含 'goals' 参考的 ctrlAddValue 控制器:function($scope, $modalInstance, goals)

感谢您的帮助!

罪魁祸首:

app.controller("ctrlCtx", function ($scope, $state, $stateParams, $modal,     $window) {

$scope.goals = "A good goal";

     $scope.addValue = function (size, $scope) {
        var modalInstance = $modal.open({
            templateUrl: 'templates/addValue.html',
            size: "lg",
            controller: "ctrlAddValue",
            resolve: {
                goals: function () {
                    return $scope.goals;
                }
            }
        })
        modalInstance.result.then(
             function (selectedItem) {

              }, 
              function () {

              });
     }; });

这是 'ctrlAddValue' 控制器代码。

app.controller('ctrlAddValue', function ($scope, $state, $modalInstance, goals) {

$scope.addValue = function(){

    $modalInstance.close();
};

$scope.cancel = function () {
    $modalInstance.dismiss();
 };

 });

这是基于模态的 ui.bootstrap 演示。

app.controller("ctrlCtx", function ($scope, $state, $stateParams, $modal,     $window) {

$scope.goals = "A good goal";

     $scope.addValue = function (size, $scope) {

您正在将 $scope 作为局部参数传递,并且可能像 $scope.addValue(something) 一样调用此函数;使局部 $scope 未定义。

从您的 addValue 函数中删除 $scope 参数。

将您的 ctrlCtx 设置为如下所示:

 app.controller("ctrlCtx", function ($scope, $state, $stateParams, $modal,     $window) {

        $scope.goals = "A good goal";

             $scope.addValue = function () {
                var modalInstance = $modal.open({
                    templateUrl: 'templates/addValue.html',
                    size: "lg",
                    controller: "ctrlAddValue",
                    resolve: {
                        goals: function () {
                            return $scope.goals;
                        }
                    }
                })

             }; 
        });

之后目标应该可以在 ctrl AddValue 中使用