Angular的一些用法或者结构技巧

如果有更好的方式,请留言交流:

2017-07-07

多个controller共用一个函数。在$rootScope中定义方法,

$rootScope.share_fun = function test_fun($scope){
  $scope.text = "123";  //修改子域的值
}

然后大调用的时候,把$scope传过去

$rootScope.share_fun($scope);

ui-sref传参数的写法:

首先你需要在html标记里写  ui-sref="content1({id:item.id})"  定义跳转,感觉ui-sref类似于a标签

然后你需要配置state,记住一定要加:id,要不然值是传不过去的。

  .state("content1", {
      url: 'tab/content1/:id',
      templateUrl: "templates/tab-content1.html",
      cache: false
  })

最后controller里面加一个$stateParams参数,这个参数里面就有id啦。

还有说说url: '/tab/content1/:id' 和 url: 'tab/content1/:id' 的区别:

如果是url: '/tab/content1/:id' 直接访问  http://localhost:46858/ionic_router/index.html?sqcode=wxsgshequ0001&snm_from=web&appcode=wxgd#/tab/content1/62046 是可以的

如果是url: 'tab/content1/:id'  (tab前面少了一个/)直接访问 http://localhost:46858/ionic_router/index.html?sqcode=wxsgshequ0001&snm_from=web&appcode=wxgd#/tab/content1/62046会跳转到

http://localhost:46858/ionic_router/index.html?sqcode=wxsgshequ0001&snm_from=web&appcode=wxgd#/tab/tab1这个主页面去。

我的理解是没有/就认为是内部跳转,一般不让你访问内部跳转,一律跳转到主页,而有/就是外部跳转,可以直接访问外部跳转。

2017-07-10

外部获取$scope作用域并修改:

   var appElement = document.querySelector('[ng-controller=published]');//获得绑定controllerdom节点
   var $scope = angular.element(appElement).scope(); //获得$scope对象
   $scope.$apply(function(){
         publish_pics_array.push(ret);
         $scope.pics_display_array.push({"url": ret});
   });//刷新数据
原文地址:https://www.cnblogs.com/angelshelter/p/7133056.html