访问控制器中ng-if范围的变量

时间:2017-11-11 21:29:41

标签: angularjs

我有一个元素:

$scope.addProduct = function () {
    console.log($scope.model.newItem);
    console.log("/order/products/new/" + $scope.newItem.id);
    $http.post("/order/products/new/" + $scope.newItem.id, null)
        .then(
            function (response) {
                $scope.currentOrder = response.data;
                $scope.apply;
            },
            function (errResponse) {
                console.log(errResponse.statusText);
            }
        )
}

我尝试在控制器中访问模型:

var findIndex = "Tomato";
$("ul li").each(function(index, item) {
  if($(item).text() == findIndex){
    console.log("Index of " + findIndex + " is: " + (index + 1));
  }
});

但模型未定义。我如何能够在控制器中访问ng-if范围的模型?

1 个答案:

答案 0 :(得分:0)

一个不那么推荐的解决方法是使用ng-show而不是ng-if,因此"隐藏" div而不是"摧毁"它。这将保留$ scope.model.newItem

<div ng-show="products.length>0"> ... </div>

这只是一个快速的解决方法,但是当div具有太多内容时会变得很重,因为隐藏它仍然会将其保留在内存中。